Collections:
OUTPUT - Defining Output Parameters in Stored Procedures in SQL Server
How To Define Output Parameters in Stored Procedures in SQL Server Transact-SQL?
✍: FYIcenter.com
Sometime a stored procedure not only want to take input values from the calling statement batch, but it also want to send output values back to the calling statement batch. This can be done by defining output parameters in the CREATE PROCEDURE statement.
To define an output parameter, you should use this format: "@parameter_name data_type OUTPUT", as shown in the following tutorial exercise:
DROP PROCEDURE diff_in_days;
GO
-- Defining an output parameter
CREATE PROCEDURE diff_in_days
@start_date DATETIME,
@end_date DATETIME = '19-May-2007',
@days VARCHAR(40) OUTPUT
AS BEGIN
SET @days = CONVERT(VARCHAR(20),@end_date,107)
+ ' - '
+ CONVERT(VARCHAR(20),@start_date,107)
+ ' = '
+ STR(DATEDIFF(DAY, @start_date, @end_date));
END;
GO
Command(s) completed successfully.
EXEC diff_in_days
@start_date='01-Jan-2007'
GO
Msg 201, Level 16, State 4, Procedure diff_in_days, Line 0
Procedure or Function 'diff_in_days' expects
parameter '@days', which was not supplied.
⇒ OUTPUT - Receiving Output Values from Stored Procedures in SQL Server
⇐ Providing Default Values to Procedure Parameters in SQL Server
2016-12-28, 2680🔥, 0💬
Popular Posts:
How To Use "IF ... ELSE IF ..." Statement Structures in SQL Server Transact-SQL? "IF ... ELSE IF ......
How To Get the Definition of a Stored Procedure Back in SQL Server Transact-SQL? If you want get the...
How To Verify a User name with SQLCMD Tool in SQL Server? The quickest way to verify a user name in ...
Why I Can Not Enter 0.001 Second in DATETIME values in SQL Server Transact-SQL? If you enter millise...
What Is SQL*Plus in Oracle? SQL*Plus is an interactive and batch query tool that is installed with e...