Stored Procedure that returns value:
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.
Syntax:
- 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