Stored Procedure With a Return Value in SQL Server 2012

Today, I have provided an article showing you how to use a return value with a Stored Procedure in SQL Server 2012. In this article we create a stored procedure to avoid duplicate record insertion in the SQL Server database prior to insertion into the database. If we insert a duplicate record in the table then execution of a stored procedure will return a status value. If we insert a record into the table which are not duplicates. The successful execution of a stored procedure will return 0. Let's take a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio. 

Return Value

Return values can be used within stored procedures to provide the stored procedure execution status to the calling program. You can create your own parameters that can be passed back to the calling program. By default, the successful execution of a stored procedure will return 0.

Now create a table named UserTable with the columns UserID, UserName. Set the identity property=true for UserID. The table looks as in the following:

img4.jpg

Now create a stored procedure:

Create PROCEDURE UsingExistsstoredprocedure

(

      @UserName VARCHAR(100)

)

AS

DECLARE @ResultValue int

BEGIN TRAN

IF EXISTS

    (

          SELECT * FROM UserTable

          WHERE UserName = @UserName

        )

     BEGIN

         SET  @ResultValue = -5

     END

ELSE

      BEGIN

           INSERT INTO UserTable

               (

                   UserName 

               )

           VALUES

           (

                 @UserName               

           )

           set @ResultValue = @@ERROR

     END

IF @ResultValue <> 0

     BEGIN

            ROLLBACK TRAN

      END

ELSE

      BEGIN

            COMMIT TRAN

      END

RETURN @ResultValue

In the preceding stored procedure we declare a return variable ResultValue. If exists is used to check the insertion record whether it belongs to the table or not. If the inserted record is already in the table then set the return status @ResultValue = -5 and the inserted record is not in the table by default; the successful execution of a stored procedure will return 0. The syntax of the return command is:

RETURN integer_value

Now hit F5 to execute the stored procedure:

img2.jpg

Now insert a value into the table using the stored procedure and check the result of executing the stored procedure with the return values as follows:

EXEC @return_variable = stored_procedure_name

Now executing the stored procedure:

DECLARE  @return_status int

EXEC @return_status= UsingExistsstoredprocedure 'Rohatash'

SELECT @return_status;

Now press F5 to run the stored procedure.

img3.jpg

Every time you insert new records it will return the same above output. Now using a select statement to select the record from the table.

img5.jpg

Now, we can execute the procedure with duplicate values to check how the RETURN statement works:

DECLARE  @return_status int

EXEC @return_status= UsingExistsstoredprocedure 'Rohatash'

SELECT @return_status;

OUTPUT

img6.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all