Output Parameters in SQL Stored Procedure

How to use Output Parameters in SQL Stored Procedure?. Or How to use Output Parameters to return a Value or message from a SQL Stored Procedure with example.

To output value from the Sql stored procedure, you have to declare a variable along with OUT or OUTPUT keyword. For this SQL Stored Procedure Output Parameters demonstration, We are going to use the below-shown SQL table

Output Parameters in SQL Stored Procedure 1

Output Parameters in SQL Stored Procedure Example 1

In this example, we show you how to use Output Parameters in a Stored procedure. Please see the Select Stored Procedure article to understand how to write Select statement inside a stored procedure. And also refer Introduction to Stored Procedure article to understand the basics of the SQL Server stored procedure.

The below statement will create a Stored procedure of Name spEmployeeCount. And we declare a variable called @EmployeeCount of integer Type along with OUTPUT keyword. Within the procedure, we are using the COUNT to find the number of employees in the Employee Duplicate table. Then we are assigning the value to the Output parameter.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO

CREATE PROCEDURE [dbo].[spEmployeeCount]
	@EmployeeCount INT OUTPUT
AS
BEGIN
	SELECT @EmployeeCount = COUNT(EmpID)FROM [EmployeeDuplicates]
END

Run query

Messages
--------
Command(s) completed successfully.

Let me execute the stored procedure. Here, we have to pass the output parameter @EmployeeCount parameter.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO
-- Declare a Variable of Type Int. Should match with SP's Output parameter
DECLARE	@TotalEmployees INT 

-- Don't forget OUTPUT keyword
EXEC  [dbo].[spEmployeeCount] @TotalEmployees OUTPUT

-- Printing the Output
PRINT @TotalEmployees
Output Parameters in SQL Stored Procedure 3

Output Parameters in SQL Stored Procedure Example 2

In this example, we explain to you how to use both Input and Output Parameters in a single Stored procedure. Please refer to Input Parameters in a Stored Procedure article to know how to use input parameters in a stored procedure.

Here, we declare a variable called @LastName as input parameter, and @EmployeeProfession of Varchar type as an Output parameter. Within the procedure, we are finding the Occupation of an employee whose last name is equal to the input parameter. Next, we are assigning the value to the Output parameter.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO

CREATE PROCEDURE [dbo].[spEmployeeProfession]
	@LastName VARCHAR(50),
	@EmployeeProfession VARCHAR(50) OUTPUT
AS
BEGIN
	SELECT @EmployeeProfession = [Occupation] FROM [EmployeeDuplicates] 
					WHERE [LastName] = @LastName
END

Run the above query

Messages
--------
Command(s) completed successfully.

Let me execute the stored procedure. Here, we have to pass the value for the @LastName as Gateway.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO

DECLARE	@Profession VARCHAR(50) 
EXEC  [dbo].[spEmployeeProfession] N'Gateway', @Profession OUTPUT
PRINT 'Gateway is Working in the ' + @Profession
Output Parameters in SQL Stored Procedure 5

Output Parameters in SQL Stored Procedure Example 3

Let us see how to use Output Parameters in a Stored procedure to return multiple outputs. Here, we declare a variable called @LastName as input parameter, and three Output parameter. Within the procedure, we are finding the Occupation, Education, and yearly Income of an employee whose last name is equal to the input parameter. Next, we are assigning the value to the Output parameter.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO

CREATE PROCEDURE [dbo].[spEmployeeProf]
	@LastName VARCHAR(50),
	@Profession VARCHAR(50) OUTPUT,
	@Education VARCHAR(50) OUTPUT,
	@Income FLOAT OUTPUT
AS
BEGIN
	SELECT @Profession = [Occupation],
		@Education = Education,
		@Income = YearlyIncome
	 FROM [EmployeeDuplicates] 
	 WHERE [LastName] = @LastName
END

Run the above query

Messages
--------
Command(s) completed successfully.

Let me execute the stored procedure.

-- Example for SQL Stored Procedure with Output Parameters
USE [SQL Tutorial]
GO

DECLARE	@EmpProfession VARCHAR(50), 
	@EmpEducation VARCHAR(50),
	@EmpIncome FLOAT
EXEC  [dbo].[spEmployeeProf] N'Gateway', @EmpProfession OUTPUT, 
                                         @EmpEducation OUT,
					 @EmpIncome OUT

SELECT @EmpProfession AS Profession,
       @EmpEducation AS Education, 
       @EmpIncome AS Income
Output Parameters in SQL Stored Procedure 7

Let me show you, What will happen if you miss the OUTPUT keyword

Output Parameters in SQL Stored Procedure 8

As you can see from the above screenshot, it is returning a NULL value. So, don’t forget the OUTPUT keyword.