Solved

using output parameter in stored procedure

Posted on 2014-02-13
3
437 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:dmaclean2
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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]
Comment Utility
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:dmaclean2
Comment Utility
Thanks much. Nice clean, simple solution.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now