Solved

how to supress the returned data row from a stored procedure

Posted on 2013-06-20
6
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

Independent Software Vendors: 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

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…
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

738 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