Solved

using output parameter in stored procedure

Posted on 2014-02-13
3
443 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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

679 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