Solved

how to supress the returned data row from a stored procedure

Posted on 2013-06-20
6
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
so, what you want the end result to be...

You want to read the 5 and leave the zero??? a bit confused here.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sdc248
Comment Utility
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
Comment Utility
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
Comment Utility
The 2nd scenario works for me. Thanks.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

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…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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