Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using output parameter in stored procedure

Posted on 2014-02-13
3
Medium Priority
?
453 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

885 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