Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - OUTPUT - Defining Output Parameters in Stored Procedures
(Continued from previous topic...)
How To Define Output Parameters in Stored Procedures?
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;
-- Defining an output parameter
CREATE PROCEDURE diff_in_days
@end_date DATETIME = '19-May-2007',
@days VARCHAR(40) OUTPUT
SET @days = CONVERT(VARCHAR(20),@end_date,107)
+ ' - '
+ ' = '
+ STR(DATEDIFF(DAY, @start_date, @end_date));
Command(s) completed successfully.
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.
(Continued on next topic...)
- What Are Stored Procedures?
- How To Create a Simple Stored Procedure?
- How To Execute a Stored Procedure?
- How To List All Stored Procedures in the Current Database?
- How To Drop an Existing Stored Procedure?
- How To Create a Stored Procedure with a Statement Block?
- How To End a Stored Procedure Properly?
- How To Generate CREATE PROCEDURE Script on an Existing Stored Procedure?
- How To Get the Definition of a Stored Procedure Back?
- How To Modify an Existing Stored Procedure?
- How To Create Stored Procedures with Parameters?
- How To Provide Values to Stored Procedure Parameters?
- What Are the Advantages of Passing Name-Value Pairs as Parameters?
- Can You Pass Expressions to Stored Procedure Parameters?
- How To Provide Default Values to Stored Procedure Parameters?
- How To Define Output Parameters in Stored Procedures?
- How To Receive Output Values from Stored Procedures?
- How To Create a Local Temporary Stored Procedure?
- Can Another User Execute Your Local Temporary Stored Procedures?