Solved

how to supress the returned data row from a stored procedure

Posted on 2013-06-20
6
271 Views
Last Modified: 2013-06-20
Hi:

I am calling a stored procedure, which returns an int, let's call it sp1, from another stored procedure, sp2. I need to store the returned value from sp1 in a variable but I don't want that returned value to be part of the returned data row of sp2. Any way I can pull this off?

Currently the following gives me two returned data set and I do not want the first one:

-- in sp2:
...
DECLARE @myvariable int
EXEC @myvariable= spMyStoredProcedure 'arg1', 'arg2'   --sp1, it returns one int
select @myvariable
...

Thanks.
0
Comment
Question by:sdc248
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39263995
if you need it to be part of the return variable then you should be put it down in the return statement at the end of the stored procedure ( remember if you put the return in the middle,the next statements wont be executed).

Check this out

create procedure sp_test1
(
 @a VARCHAR(50),@b VARCHAR(50)
)
as
BEGIN
declare @t INT 
set @t = 5 -- This can be set to any value or programatically set down the stream.
-- do something or some selects, updates, deletes 
return @t
END


Create procedure sp_test2
as
BEGIN
-- do something or nothing
DECLARE @myvariable int
exec @myvariable = dbo.sp_test1 'a','B' 
select @myVariable.
END

Open in new window

0
 

Author Comment

by:sdc248
ID: 39264190
Looks like my problem is that the return value from sp1 (sp_test1 in your example) was actually from a SELECT statement instead of a RETURN statement. That is, if I change the last statement in sp_test1 to be "select @t", I'd get '5' as a data row followed by a '0' as a 2nd data row from sp_test2.  

Anything I can do in sp_test2 to resolve the issue without changing sp_test1?
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39264224
so, what you want the end result to be...

You want to read the 5 and leave the zero??? a bit confused here.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:sdc248
ID: 39264261
Assuming the last statement in sp_test1 was "select @t", I want sp_test2 to return 5 and only 5.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39264358
scenario 1:
where you want the 5 as a result set ( as a row in sql server)

create procedure sp_test1
(
 @a VARCHAR(50),@b VARCHAR(50)
)
as
BEGIN
declare @t INT 
set @t = 5 -- This can be set to any value or programatically set down the stream.
-- do something or some selects, updates, deletes 
select @t
END


Create procedure sp_test2
as
BEGIN
-- do something or nothing
exec dbo.sp_test1 'a','B' 
END

Open in new window


Scenario 2: Where you want 5 to be as a return value

create procedure sp_test1
(
 @a VARCHAR(50),@b VARCHAR(50)
)
as
BEGIN
declare @t INT 
set @t = 5 -- This can be set to any value or programatically set down the stream.
-- do something or some selects, updates, deletes 
select @t
END


Create procedure sp_test2
as
BEGIN
-- do something or nothing
declare @MyVariable INT
DECLARE @T table
(
 returnValue INT
)
insert into @T 
exec dbo.sp_test1 'a','B' 

select @myVariable = returnValue FRom @t
return @myVariable
END

Open in new window

0
 

Author Closing Comment

by:sdc248
ID: 39264374
The 2nd scenario works for me. Thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server stored procedure parameter 10 20
Change this SQL to get all nodes 3 36
SQL Select Query help 1 34
SQL 2014 missing dll from Bin? 3 31
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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

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