Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Return Values in SQL Stored Procedure

by suresh

How to use Return Values in SQL Stored Procedure?. Or How to use Return Values to return a message from a SQL Stored Procedure with example.

Return Values in a Stored Procedure in Sql Server will return integer values only. By default, it returns 0, if you execute any stored procedure successfully. For this SQL stored procedure return output demonstration, We are going to use the below-shown SQL table

I suggest you refer Introduction to Stored Procedure article to learn the basics of the SQL Server stored procedure.

Return Values in SQL Stored Procedure 1

Return Values in SQL Stored Procedure Example 1

In this example, we will show you the default return value returned by the SQL Server. To demonstrate the same, let me execute the previously created Stored Procedure. Please refer to Input Parameters in a Stored procedure article to know the definition behind this stored procedure.

Return Values in SQL Stored Procedure 2

Here, we are assigning the Occupation value as Management.

Return Values in SQL Stored Procedure 3

Once you click on the OK button, a new query window will open with the following auto-generated code. As you can see, it is returning 0 as a return value. 0 means successful.

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

DECLARE	@return_value int

EXEC	@return_value = [dbo].[spGetEmployeeDetails]
		@Occupation = N'Management'

SELECT	'Return Value' = @return_value

GO

OUTPUT

Return Values in SQL Stored Procedure 4

Return Values in SQL Stored Procedure Example 2

In this return value example, we will show how to use return values in Stored procedures. Please refer to the Select Stored Procedure article to write Select statement inside a stored procedure.

Within the procedure, we are using the COUNT to find the number of employees in the Employee Duplicate table, and then we are returning the value.

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

CREATE PROCEDURE [dbo].[spCountEmployees]
AS
BEGIN
	RETURN (SELECT COUNT(EmpID)FROM [EmployeeDuplicates])
END

OUTPUT

Return Values in SQL Stored Procedure 5

Let me execute the stored procedure.

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

-- Assigning the Output of a Stored Procedure to Declared variable
EXEC @TotalEmployees = [dbo].[spCountEmployees]

-- Printing the Output
PRINT @TotalEmployees

OUTPUT

Return Values in SQL Stored Procedure 6

Stored Procedure return values Example 3

In this example, Let me show you what will happen If we return a String (Varchar) data as the return value.

Here, we are selecting the Occupation of an employee whose last name is equal to Gateway. (Instead of hard coding a Value, you can also try Input Parameter).

-- Example for SQL Stored Procedure Return Values
USE [SQL Tutorial]
GO

CREATE PROCEDURE [dbo].[spEmployeeName]
AS
BEGIN
 RETURN (SELECT [Occupation] FROM [EmployeeDuplicates]
         WHERE [LastName] = N'Gateway')
END

OUTPUT

Return Values in SQL Stored Procedure 7

Let me execute the stored procedure.

-- Example for SQL Stored Procedure Return Values
USE [SQL Tutorial]
GO
DECLARE	@EmpName VARCHAR(50) 
EXEC @EmpName = [dbo].[spEmployeeName]
PRINT @EmpName

OUTPUT

Return Values in SQL Stored Procedure 8

As you can see from the above screenshot that it is throwing an error. because Return Values always return integers only.

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy