Solved

using output parameter in stored procedure

Posted on 2014-02-13
3
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

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 143

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

623 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