User-Defined Functions in Sql Server SSMS

  • Thread starter Thread starter WWGD
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 4K views
Messages
7,819
Reaction score
13,151
TL;DR
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?
 
on Phys.org
jedishrfu said:
are you missing the RETURN stmt:

RETURN SQRT(@a*@a + @b*@b);
Excellent, that did it. My Programmability folder runneth over. Thanks.
 
Reply
  • Like
Likes   Reactions: 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.
 
Reply
  • Like
Likes   Reactions: WWGD
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.
 
Reply
  • Like
Likes   Reactions: WWGD