Solved

how to supress the returned data row from a stored procedure

Posted on 2013-06-20
6
269 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

810 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