MS SQL Server Concepts and Programming Question:
Download Job Interview Questions and Answers PDF
Can You Pass Expressions to Function Parameters?
Answer:
Can you pass expressions to stored procedure parameters? The answer is yes.
When executing functions, input values can be written as expressions. But the resulting value data type must match the parameter. The tutorial exercise below shows you how input values should be specified:
CREATE FUNCTION Area(@radius REAL)
RETURNS REAL
AS BEGIN
RETURN 3.14*@radius*@radius;
END;
GO
-- Input value data matches the parameter
PRINT 'Area of a circle: '+STR(dbo.Area(1.5),9,3);
GO
Area of a circle: 7.065
-- Input value data does not match the parameter
PRINT 'Area of a circle: '+STR(dbo.Area('1.5'),9,3);
GO
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Area'.
-- Expressions are allowed
PRINT 'Area of a circle: '+STR(dbo.Area(1.0+0.5),9,3);
GO
Area of a circle: 7.065
When executing functions, input values can be written as expressions. But the resulting value data type must match the parameter. The tutorial exercise below shows you how input values should be specified:
CREATE FUNCTION Area(@radius REAL)
RETURNS REAL
AS BEGIN
RETURN 3.14*@radius*@radius;
END;
GO
-- Input value data matches the parameter
PRINT 'Area of a circle: '+STR(dbo.Area(1.5),9,3);
GO
Area of a circle: 7.065
-- Input value data does not match the parameter
PRINT 'Area of a circle: '+STR(dbo.Area('1.5'),9,3);
GO
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Area'.
-- Expressions are allowed
PRINT 'Area of a circle: '+STR(dbo.Area(1.0+0.5),9,3);
GO
Area of a circle: 7.065
Download MS SQL Server Interview Questions And Answers
PDF
Previous Question | Next Question |
How To Provide Values to User Defined Function Parameters? | How To Provide Default Values to Function Parameters? |