Solved

using output parameter in stored procedure

Posted on 2014-02-13
3
440 Views
Last Modified: 2014-02-14
I am probably missing something very basic. I have a 'main' stored procedure that calls several other stored procedures, each of which do inserts and updates to various tables.

I want to add some logic to each subsidiary procedure to output some result information to the main procedure and I don't want to be limited to an integer. An Output Parameter seems to make sense but I have not been able to make it work.

Attached is a very simple example of what I want to be able to do with one trivial stored procedure calling another one that has an output parameter.

Here's all the code

CREATE PROCEDURE [dbo].[ProcWithOutput]
  @ValueA nvarchar(100), @ValueB nvarchar(100), @OutParam nvarchar(10) OUTPUT
AS
BEGIN
      IF @ValueA = @ValueB BEGIN
            SET @OutParam = 'Good'
      END
      ELSE BEGIN
            SET @OutParam = 'Bad'
      END
END
GO

CREATE PROCEDURE [dbo].[CallProcWithOutput]
AS
BEGIN
DECLARE      @return_value int
DECLARE @A nvarchar(100) = 'Boston'
DECLARE @B nvarchar(100) = 'New York'
DECLARE @TxtResult nvarchar(10) = NULL
      EXEC @return_value = [dbo].[ProcWithOutput] @A, @B, @TxtResult
      SELECT @TxtResult AS ResponseFromProcWithOutput
END
GO

DECLARE      @return_value int
EXEC      @return_value = [dbo].[CallProcWithOutput]
SELECT      'Return Value' = @return_value
GO

/*
DROP PROCEDURE ProcWithOutput
DROP PROCEDURE CallProcWithOutput
*/

The result I actually get from the SELECT statement is NULL
SPResult.JPG
Output-example-to-Experts-Exchan.sql
0
Comment
Question by:Douglass MacLean
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39858352
change this line to append OUTPUT keyword, that will then work
EXEC @return_value = [dbo].[ProcWithOutput] @A, @B, @TxtResult OUTPUT
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39858354
just a note:
 SELECT @TxtResult AS ResponseFromProcWithOutput

this will NOT assign the value to the @return_value !!!
first the return value can only be INT as data type, and to assign a value there, it would have to be
RETURN <something>
and not SELECT <something> ...

hope this clarifies
0
 

Author Closing Comment

by:Douglass MacLean
ID: 39858415
Thanks much. Nice clean, simple solution.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question