12

I have a stored procedure that I want to call from within another, and then loop through the results. Sort of like using a cursor with a stored procedure rather than a SQL select statement. I can't quite figure out how to do it.

I can get the whole result like this:

DECLARE @result int;
EXEC @result = sp_who;
PRINT @result;

Interestingly, this seems to change the type of @result to something other than int, but whatever. How do I then loop through the results, row by row? How do access the data from the individual columns? For example, how would I kill processes where the forth column (loginname) is like '%gatesb' or whatever?

1

4 Answers 4

13

You would declare a table variable to hold the results of the stored procedure and then loop through them in a while loop:

declare @temp table (
    idx int identity(1,1),
    field1 int,
    field2 varchar(max))

declare @result int

insert into @temp (field1, field2)
exec @result = sp_who

declare @counter int

set @counter = 1

while @counter < (select max(idx) from @temp)
begin
    -- do what you want with the rows here
    set @counter = @counter + 1
end
4
  • Sweet - that works. Just need to replace '@temp' with '#temp' and 'exec @result = sp_who' to 'exec sp_who'. Thanks for that.
    – TallGuy
    May 14, 2009 at 2:36
  • 1
    You should be able to use a table variable [@temp] rather than a temp table [#temp]. If you have to use a temp table, make sure you drop the table when you're done with it...otherwise it'll hang around. May 14, 2009 at 11:33
  • What do I declare the Result as? Must declare the scalar variable "@result".
    – PeterX
    Feb 7, 2018 at 7:02
  • @PeterX - you'd declare the scalar variable as integer, which is what SQL Server stored procedures return. If all goes well with the stored procedure you're calling, it will return 0, unless its written to return something other than 0. On the other hand, if while the sproc is executing it encounters an error, the returned value will be non-zero.
    – STLDev
    May 17, 2018 at 1:34
3

Rewrite sp_who as a table function

2
  • 3
    I assume you mean, "Rewrite your stored proc as a table value function." I don't think reimplementing a system stored proc is such a good idea ;)
    – onedaywhen
    May 14, 2009 at 7:37
  • 1
    I used this method to solve my problem - very clean.
    – qxotk
    Feb 22, 2019 at 23:19
3

you can catch the results of a stored proc by inserting into a table that has matching columns...

create table #spWhoResults
    (spid smallint,
    ecid smallint,
    status nchar(60),
    loginame nchar(256),
    hostname nchar(256),
    blk char(5),
    dbname nvarchar(128),
    cmd nchar(32),
    request_id int)

go

insert  #spWhoResults
exec    sp_who


select  *
from    #spWhoResults

/* 
put your cursor here to loop #spWhoResults to 
perform whatever it is you wanted to do per row
*/
0

What Justin pointed out is what you have to do, but instead of doing

while @counter < (select max(idx) from @temp)

do this

declare @maxid int
select @maxid = max(idx), @counter = 1
from @temp
while @counter < @maxid begin
-- go on
  set @counter = @counter + 1
end

Also, if declaring the table as @temp doesn't work you could declare it as #temp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.