February 16, 2016 20:15 by
Peter
In this tutorial, I will show you how to Return Value in SQL Server from EXEC Function. If you specify the OUTPUT keyword for a parameter in the procedure definition, the procedure can return the current value of the parameter to the calling program when the procedure exits. The following Stored Procedure is used which returns an Integer value 1 if the StudentId exists and 0 if the StudentId does not exists.
CREATE PROCEDURE CheckStudentId
@StudentId INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Exists INT
IF EXISTS(SELECT StudentId FROM Students WHERE StudentId = @StudentId)
BEGIN
SET @Exists = 1
END
ELSE
BEGIN
SET @Exists = 0
END
RETURN @Exists
END
Returned value from EXEC function:
The returned integer value from the Stored Procedure, you need to make use of an Integer variable and use along with the EXEC command while executing the Stored Procedure.
DECLARE @ReturnValue INT
EXEC @ReturnValue = < Store Procedure Name > < Parameters > Select @ReturnValue
Example: DECLARE @ReturnValue INT
EXEC @ReturnValue = CheckStudentId 34
SELECT @ReturnValue
If There are valid StudentId then Output will be : 1
HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.