?
Solved

Iterating through a record set to run SP

Posted on 2014-01-19
4
Medium Priority
?
287 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
[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
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 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 1000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

719 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