Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I have a query which returns a column of ints, for each row, I want to call a stored procedure, how can this be done?

Posted on 2009-05-15
1
Medium Priority
?
212 Views
Last Modified: 2012-05-07
I have a query which returns a column of ints, for each row, I want to call a stored procedure, how can this be done?

Let's say i have 'select my_key from my_table where cool=1'  and it returns:

2
3
6
8
9
12

from that, i would like to automatically run

EXEC my_sp @p1=2
EXEC my_sp @p1=3
EXEC my_sp @p1=6
EXEC my_sp @p1=8
EXEC my_sp @p1=9
EXEC my_sp @p1=12

Can I do something like

EXEC my_sp @p1=(select my_key from my_table where cool=1) or is more complicated than that?

Assume that my_key and @p1 are both type int
0
Comment
Question by:Matt_Du
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24400129
this should do:
declare @t table ( id int )
declare @id int 
insert into @t select my_key from my_table where cool=1
while @@rowcount > 0
begin
  select top 1 @id = id from @t
  if @@rowcount > 0
  begin
    exec my_sp @p1 = @id
  end
  delete @t where id = @id
  
end

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

824 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