MS SQL Server Concepts and Programming Question:
Download Job Interview Questions and Answers PDF
How To Create User Defined Functions with Parameters?
Answer:
Very often, you need to create a function with one or more parameters so that the function can be more generic. You only supply values to those parameters at the time of executing the function.
User defined functions with parameters can be created with the following syntax:
CREATE FUNCTION function_name (
@parameter_1 data_type,
@parameter_2 data_type,
...
@parameter_n data_type
)
RETURNS data_type
AS BEGIN
statement_1;
statement_2;
...
statement_n;
END;
The following tutorial exercise shows you how to create a function with one parameter called @url:
USE GlobalGuideLine;
GO
DROP FUNCTION Welcome;
GO
CREATE FUNCTION Welcome(@url VARCHAR(40))
RETURNS VARCHAR(40)
AS BEGIN
RETURN 'Welcome to '+@url;
END;
GO
PRINT 'Hi there, '+dbo.Welcome('GlobalGuideLine.com');
GO
Hi there, Welcome to GlobalGuideLine.com
User defined functions with parameters can be created with the following syntax:
CREATE FUNCTION function_name (
@parameter_1 data_type,
@parameter_2 data_type,
...
@parameter_n data_type
)
RETURNS data_type
AS BEGIN
statement_1;
statement_2;
...
statement_n;
END;
The following tutorial exercise shows you how to create a function with one parameter called @url:
USE GlobalGuideLine;
GO
DROP FUNCTION Welcome;
GO
CREATE FUNCTION Welcome(@url VARCHAR(40))
RETURNS VARCHAR(40)
AS BEGIN
RETURN 'Welcome to '+@url;
END;
GO
PRINT 'Hi there, '+dbo.Welcome('GlobalGuideLine.com');
GO
Hi there, Welcome to GlobalGuideLine.com
Download MS SQL Server Interview Questions And Answers
PDF
Previous Question | Next Question |
How To Modify an Existing User Defined Function? | How To Provide Values to User Defined Function Parameters? |