Solved

Iterating through a record set to run SP

Posted on 2014-01-19
4
268 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
Comment Utility
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
Comment Utility
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
Comment Utility
Surendra  -

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

Author Closing Comment

by:ccleebelt
Comment Utility
Thanks. Figured it out based on your feedback, thanks.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now