Solved

Iterating through a record set to run SP

Posted on 2014-01-19
4
274 Views
Last Modified: 2014-01-19
I should probably know this but I don't.

I have a records set, let's keep it simple

Select email_address from contacts where email_address is not null

It returns a list of rows. duh.

Now, I want to iterate through the records set and do some for each.  

--Start loop

Exec sp_do_something 'email_address"

--end loop

What is the proper syntax to do this in MSSQL (replace the start loop and end loop.
0
Comment
Question by:ccleebelt
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 39793199
declare @emails table (i int identity, EmailAddress varchar(100) )
declare @I int, @EmailAddress varchar(100)

insert into @emails
Select email_address from contacts where email_address is not null
select @I = @@rowcount

While @I >  0
BEGIN
   SELECT @EmailAddress = EmailAddress, @I = @I -1 from @Emails
   EXEC ProcDoSomething @EmailAddress
END
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 250 total points
ID: 39793234
the logic stays as aneesh said above, but it is better to use curosors or just a where clause to what aneesh did above

declare @t Cursor 
for
select email_address from <your table>
open @t
FETCH @t into @email
while @@FETCH_STATUS = 0
BEGIN
  EXEC <your Proc> @email
  FETCH @t into @email
END

Open in new window

0
 

Author Comment

by:ccleebelt
ID: 39793266
Surendra  -

Not seeing how to combine the two solutions into a working solution.  Can you elaborate?
0
 

Author Closing Comment

by:ccleebelt
ID: 39793291
Thanks. Figured it out based on your feedback, thanks.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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…

809 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