User-Defined Functions in Sql Server SSMS

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

Discussion Overview

The discussion revolves around writing 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. Participants explore syntax issues, error handling, and potential numerical concerns related to the function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a function definition but encounters a syntax error related to the missing RETURN statement.
  • Another participant suggests the inclusion of the RETURN statement to resolve the syntax error.
  • A later reply humorously references a cultural example, comparing the situation to a character from "The Wizard of Oz" misapplying the Pythagorean theorem.
  • Concerns are raised about potential numerical overflow when using real numbers, suggesting the need for error handling in the function.
  • Participants discuss the use of TRY CATCH for error handling in MS SQL, noting that the approach may vary based on specific circumstances.

Areas of Agreement / Disagreement

Participants generally agree on the need for a RETURN statement to fix the syntax error. However, there is no consensus on the best approach for error handling, as opinions vary on how to implement it effectively.

Contextual Notes

Participants mention potential numerical overflow issues without providing specific solutions, indicating a need for further exploration of error handling techniques in SQL.

Who May Find This Useful

Individuals interested in SQL programming, particularly those working with user-defined functions and error handling in SQL Server.

WWGD
Science Advisor
Homework Helper
Messages
7,785
Reaction score
13,076
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
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 80 ·
3
Replies
80
Views
10K