User-Defined Functions in Sql Server SSMS

  • Thread starter Thread starter WWGD
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on creating a user-defined function in SQL Server Management Studio (SSMS) that calculates the square root of the sum of the squares of two real numbers. The correct syntax requires a RETURN statement, specifically: RETURN SQRT(@a * @a + @b * @b);. Additionally, the conversation highlights the potential for numerical overflow when dealing with real numbers and suggests implementing error handling using TRY CATCH. The participants emphasize the importance of proper syntax and error management in SQL Server functions.

PREREQUISITES
  • Understanding of SQL Server Management Studio (SSMS)
  • Familiarity with user-defined functions in SQL Server
  • Knowledge of SQL data types, specifically Real
  • Basic error handling techniques in SQL Server using TRY CATCH
NEXT STEPS
  • Research SQL Server user-defined functions and their syntax
  • Learn about error handling in SQL Server using TRY CATCH
  • Explore numerical overflow issues in SQL data types
  • Study best practices for writing robust SQL functions
USEFUL FOR

Database developers, SQL Server administrators, and anyone involved in writing or optimizing user-defined functions in SQL Server.

WWGD
Science Advisor
Homework Helper
Messages
7,779
Reaction score
13,022
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?
 
Technology news on Phys.org
are you missing the RETURN stmt:

RETURN SQRT(@a*@a + @b*@b);
 
  • Like
Likes   Reactions: 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   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.
 
  • 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.
 
  • Like
Likes   Reactions: WWGD

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
13
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 80 ·
3
Replies
80
Views
10K