In this tutorial, I will explain how to pass an output parameter to a stored procedure in MS SQL Server and also, we will see how to use stored procedure in SQL Server with an output parameter.

So, we will write the stored procedure for inserting the data for demonstration.
Stored Procedure in SQL Server
  1. USE [DB_MANTRY]
  2. --CREATED ON 08/12/2017 BY NIKUNJ SATASIYA
  3. CREATE PROCEDURE BL_UserInfo_Ins -- BL_UserInfo_Ins is Procedure Name
  4. @UserName VARCHAR(50) ,
  5. @Password VARCHAR(50) ,
  6. @FirstName VARCHAR(50) ,
  7. @LastName VARCHAR(50) ,
  8. @Email VARCHAR(50) ,
  9. @Location VARCHAR(50) ,
  10. @Created_By VARCHAR(50) ,
  11. @ReturnValue INT = 0 OUT
  12. AS
  13. BEGIN
  14. -- SET NOCOUNT ON added to prevent extra result sets from
  15. -- interfering with SELECT statements.
  16. SET NOCOUNT ON ;
  17. ---Condition For Check if User exists or not if user does not exist then returns different message if exists returns different message
  18. IF NOT EXISTS ( SELECT * FROM BL_User_Info WHERE UserName = @UserName )
  19. BEGIN
  20. INSERT INTO BL_User_Info
  21. ( UserName ,
  22. [Password] ,
  23. FirstName ,
  24. LastName ,
  25. Email ,
  26. Location ,
  27. Created_By
  28. )
  29. VALUES ( @UserName ,
  30. @Password ,
  31. @FirstName ,
  32. @LastName ,
  33. @Email ,
  34. @Location ,
  35. @Created_By
  36. )
  37. --If User Successfully Registerd then we will return this Message as Output Parameter
  38. --SET @ReturnValue = 0
  39. SET @ReturnValue = @UserName + ' is Registered Successfully'
  40. END
  41. ELSE
  42. BEGIN
  43. --If User already Exists We will return this Message as Output Parameter
  44. --SET @ReturnValue = 1
  45. SET @ReturnValue = @UserName + ' is Already Exists'
  46. END
  47. END
You can see the created stored procedure where we are sending @ReturnValue as an output parameter. And, it shows the appropriate message to the user based on the return value.