SQL Server 2016 Hosting – HostForLIFE.eu :: How To Pass Output Parameter To Stored Procedure In SQL Server?
|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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
USE [DB_MANTRY] CREATE PROCEDURE BL_UserInfo_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 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON ; ---Condition For Check if User exists or not if user does not exist then returns different message if exists returns different message IF NOT EXISTS ( SELECT * FROM BL_User_Info WHERE UserName = @UserName ) BEGIN INSERT INTO BL_User_Info ( UserName , [Password] , FirstName , LastName , Email , Location , Created_By ) VALUES ( @UserName , @Password , @FirstName , @LastName , @Email , @Location , @Created_By ) --If User Successfully Registerd then we will return this Message as Output Parameter --SET @ReturnValue = 0 SET @ReturnValue = @UserName + ' is Registered Successfully' END ELSE BEGIN --If User already Exists We will return this Message as Output Parameter --SET @ReturnValue = 1 SET @ReturnValue = @UserName + ' is Already Exists' END 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.