1. Using Output Parameters
If a calling batch passes a variable as a
parameter to a stored procedure and that parameter is modified inside
the procedure, the modifications are not passed to the calling batch
unless you specify the OUTPUT keyword for the parameter when executing the stored procedure.
If you want a procedure to be able to pass parameter values out from the procedure, you need to use the keyword OUTPUT
when creating the procedure. The following example shows a stored
procedure that accepts two parameters, one of which is used as an output
parameter:
IF EXISTS ( SELECT * FROM sys.procedures
WHERE schema_id = schema_id('dbo')
AND name = N'ytd_sales')
DROP PROCEDURE dbo.ytd_sales
GO
CREATE PROC ytd_sales
@title varchar(80), @ytd_sales int OUTPUT
AS
SELECT @ytd_sales = ytd_sales
FROM titles
WHERE title = @title
RETURN
The calling batch (or stored procedure) needs to declare a variable to store the returned value. The execute statement must include the OUTPUT keyword as well, or the modifications won’t be reflected in the calling batch’s variable:
DECLARE @sales_up_to_today int
EXEC ytd_sales 'Life Without Fear', @sales_up_to_today OUTPUT
PRINT 'Sales this year until today''s date: ' +
CONVERT(VARCHAR(10), @sales_up_to_today) + '.'
Sales this year until today's date: 111.
You can also pass the output parameter by name:
DECLARE @sales_up_to_today int
EXEC ytd_sales 'Life Without Fear',
@ytd_sales = @sales_up_to_today OUTPUT
PRINT 'Sales this year until today''s date: ' +
CONVERT(VARCHAR(10), @sales_up_to_today) + '.'
Note that when you pass an output parameter by name, the parameter name (@ytd_sales, in this example) is listed on the left side of the expression, and the local variable (@sales_up_to_today),
which is set equal to the value of the output parameter, is on the
right side of the expression. An output parameter can also serve as an
input parameter.
Output parameters can also be passed back and captured in a client application by using ADO, ODBC, OLE DB, and so on.
2. Returning Procedure Status
Most programming languages are able to pass a status code to the caller of a function or subroutine. A value of 0 generally indicates that the execution was successful. SQL Server stored procedures are no exception.
SQL Server automatically generates an integer status value of 0 after successful completion of a stored procedure. If SQL Server detects a system error, a status value between -1 and -99 is returned. You can use the RETURN statement to explicitly pass a status value less than -99 or greater than 0. The calling batch or procedure can set up a local variable to retrieve and check the return status.
In Listing 1,
the stored procedure returns the year-to-date sales for a given title
as a result set. If the title does not exist, to avoid returning an
empty result set, the procedure returns the status value -101.
In the calling batch or stored procedure, you need to create a variable
to hold the return value. The variable name is passed the EXECUTE keyword and the procedure name as shown in Listing 28.16.
Listing 1. Returning a Status Code from a Stored Procedure
IF EXISTS ( SELECT * FROM sys.procedures
WHERE schema_id = schema_id('dbo')
AND name = N'ytd_sales2')
DROP PROCEDURE dbo.ytd_sales2
GO
--Create the procedure
CREATE PROC ytd_sales2 @title varchar(80)
AS
IF NOT EXISTS (SELECT * FROM titles WHERE title = @title)
RETURN -101
SELECT ytd_sales
FROM titles
WHERE title = @title
RETURN
GO
-- Execute the procedure
DECLARE @status int
EXEC @status = ytd_sales2 'Life without Fear'
IF @status = -101
PRINT 'No title with that name found.'
go
ytd_sales
---------
111
-- Execute the procedure
DECLARE @status int
EXEC @status = ytd_sales2 'Life without Beer'
IF @status = -101
PRINT 'No title with that name found.'
go
No title with that name found.
|
Return values can also be passed back and captured by client applications developed in ADO, ODBC, OLE DB, and so on.