User-Defined Functions in Sql Server SSMS

  • Thread starter Thread starter WWGD
  • Start date Start date
AI Thread Summary
A user is attempting to create a user-defined function in SQL Server Management Studio (SSMS) to calculate the square root of the sum of the squares of two real numbers. The initial function definition lacks a RETURN statement, leading to a syntax error. The correct implementation includes a RETURN statement: "RETURN SQRT(@a * @a + @b * @b);". After this adjustment, the user successfully resolves the issue. Additionally, there are concerns about potential numerical overflow due to the nature of real numbers, suggesting the need for error handling. Recommendations include using SQL Server's TRY CATCH for error management and seeking further coding assistance from online resources or AI chatbots.
WWGD
Science Advisor
Homework Helper
Messages
7,679
Reaction score
12,489
TL;DR Summary
Trying to figure out syntax problem with Syntax for function that takes as inputs two Real number and outputs the square root of the sum of the squares of the two numbers.
Hi, trying to write a user-defined function in SSMS Sql Server .that takes two Real numbers and outputs the square root of the
sum of their squares:

Line 152
CREATE FUNCTION dbo.Distance(@a Real, @b Real)
RETURNS Real
AS
BEGIN
SQRT(@a * @a + @b * @b)
END ;
GO

I re-checked the syntax for user-defined functions, but somehow I keep getting error messages .
Error Message:
Msg 102, Level 15, State 1, Procedure Distance, Line 5 [Batch Start Line 152]
Incorrect syntax near 'SQRT'.

any ideas?
 
Technology news on Phys.org
are you missing the RETURN stmt:

RETURN SQRT(@a*@a + @b*@b);
 
  • Like
Likes harborsparrow and WWGD
jedishrfu said:
are you missing the RETURN stmt:

RETURN SQRT(@a*@a + @b*@b);
Excellent, that did it. My Programmability folder runneth over. Thanks.
 
  • Like
Likes harborsparrow and jedishrfu
Because these are reals, depending on the relative size of input values, this computation might be subject to numerical overflow. So it might be helpful to insert some error handling in there.
 
harborsparrow said:
Because these are reals, depending on the relative size of input values, this computation might be subject to numerical overflow. So it might be helpful to insert some error handling in there.
Thanks, any refs?
 
MS SQL uses TRY CATCH, but what to do if an error occurs depends on so many things. I would say Google it. You might also ask an AI chatbot for help coding that.
 

Similar threads

Replies
11
Views
3K
Replies
8
Views
1K
Replies
4
Views
6K
Replies
2
Views
3K
2
Replies
80
Views
9K
Back
Top