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
199 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
[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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL to JSON 14 65
TSQL Assignining CTE column in to a variable 3 27
STUFF function - proper use to update column? 6 18
Query Task 8 23
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.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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