Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Iterating through a record set to run SP

Posted on 2014-01-19
4
Medium Priority
?
293 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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 ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

926 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