Stored Procedure: Return True if Record Exists and False if Record does not Exist in SQL Server

The following Stored Procedure is used which returns an Integer value 1 if the StudentId exists and 0 if the StudentId does not exists. 
  1. Create Procedure CheckStudentId(@StudentId int)  
  2.  As  
  3.    
  4. Begin  
  5.    
  6. Declare @Exist int  
  7.    
  8. IF Exist( Select StudentId From Student Where StudentId=@StudentId)  
  9.    
  10. Begin  
  11.        Set @Exist = 1   
  12. End  
  13.    
  14. Else  
  15.    
  16. Begin  
  17.        Set @Exist=0  
  18. end  
  19.    
  20. Return @Exist  
  21.    
  22. end    
Execute The Store Procedure: 

In order to fetch 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:
  1. Declare @ReturnValue Int  
  2. Exec @ReturnValue=<Store Procedure Name> <Parameters>  
  3. Select @ReturnValue  
Example:
  1. Declare @ReturnValue  
  2. Exec @ReturnValue = CheckStudentId 1  
  3. Select @ReturnValue