Solved

Iterating through a record set to run SP

Posted on 2014-01-19
4
277 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 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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