How to Pass Output Parameter to Stored Procedure in Microsoft SQL Server

watch_later 26 November, 2018


In This article i am going to explain how you can pass an output parameter to stored procedure in Microsoft SQL Server and i also explain how you can use stored procedure with output parameter in MS Sql Server. In my previous article i explained How create dynamic stored procedure in MS Sql Server and also explained How to Create and Execute Parameterise Stored Procedure From Another Stored Procedure.

So, In this article will show you how to create stored procedure for insert and update record within table using output parameter in MS Sql Server.

Today, I got a new request from my client, actually they said he need a form and wants to validate that form dynamically from database side. 

Actually my client said in any situation if they want to add/change/remove any conditions/validation then they can add/change/remove directly from database side they didn't wants to do any changes from code side and also wants to display appropriate validation message from database side to end user so even they also can change validation message also from database side without change code of web page. 

So, i have wrote store procedure for archive given requirement and that i wanna share with you.


1) Create Stored Procedure For Insert and Update Record Within Table in Sql Server.
2) Add Validation Within Created Stored Procedure In Sql Server.
3) Pass an Output Parameter to Stored Procedure in Microsoft SQL Server
4) Return Validation Message using Output Parameter.


USE [DB_MyDatabase]  
CREATE PROCEDURE LKS_UserDetails_Ins -- BL_UserInfo_Ins is Procedure Name  
    @UserName VARCHAR(50) ,  
    @Password VARCHAR(50) ,  
    @FirstName VARCHAR(50) ,  
    @LastName VARCHAR(50) ,  
    @Email VARCHAR(50) ,  
    @Location VARCHAR(50) ,  
    @Created_By VARCHAR(50) ,  
  --  @ReturnValue INT = 0 OUT  
   @ReturnValue VARCHAR(50) = '' OUT  
-- SET NOCOUNT ON added to prevent extra result sets from  
-- interfering with SELECT statements.  
        SET NOCOUNT ON ;  
  IF EXISTS (SELECT 1 FROM LKS_UserDetails WHERE UserName = @UserName) 
   SET @ReturnValue = @UserName + ' is Already Exists, Please Try Different Username.'  
  IF EXISTS (SELECT 1 FROM LKS_UserDetails WHERE Email = @Email) 
   SET @ReturnValue = @Email + ' is Already Exists, Please Try Different Email.'  
  --- This is Condition To Check wather User is exists or not if user dose not exist within table then returns validation message else returns different validation message  
        IF NOT EXISTS ( SELECT 1 FROM LKS_UserDetails  WHERE   UserName = @UserName AND Email = @Email)   
                INSERT  INTO BL_User_Info  
                        ( UserName ,  
                          [Password] ,  
                          FirstName ,  
                          LastName ,  
                          Email ,  
                          Location ,  
                VALUES  ( @UserName ,  
                          @Password ,  
                          @FirstName ,  
                          @LastName ,  
                          @Email ,  
                          @Location ,  
    --Messege for User Successfully Registerd 
                --SET @ReturnValue = 0  
                SET @ReturnValue = @UserName + ' is Registered Successfully'  
   ------ You can Write Update Statment if You want based on your Requirement 
   ------ Hear i just Return if user already exists
                --SET @ReturnValue = 1  
                SET @ReturnValue = @UserName + ' is Already Exists'  
If you analysed above stored procedure then i have declared output parameter with name @ReturnValue VARCHAR(50) ='' OUT as Output parameter and set appropriate message to declared output parameter after successful insert and update operation. and also check validation before insert and update record and set validation message to output parameter.
Output Parameter Sql Server
Output Parameter Sql Server


This article explains how to use output parameter in Sql Server and how to check validation dynamically from database side without change any code. 

If You have such sql tips/tricks and any new things then you can share with me, i like to learn new things and also i will post your article with your credit.

I hope this article will helps you to archive your requirement,  If you have any questions and need any help then please you can leave your comments on comment box.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.

sentiment_satisfied Emoticon