467,915 Members | 1,267 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Loop on results, call sproc

jkmyoung
Expert 2GB
Hi,
I'm looking for a way to loop on a set of temporary results and run a stored procedure on each row of the results.

Simplified code of what I'm trying to do:
Expand|Select|Wrap|Line Numbers
  1. Declare @TMP TABLE
  2. (
  3. field1 Int
  4. field2 Int
  5. )
  6.  
  7. INSERT INTO @TMP
  8. SELECT id, otherVar
  9. from ....blah join blah join blah
  10. where bar = foo
  11.  
  12.  
  13. Declare @RC = int
  14. -- Want to loop here.
  15. For each result.
  16. Exec @RC = [Process].[dbo].[usp.storedProc1]   **field1** **field2**
  17. End for
  18.  
What is the syntax to do this? Also is this terminology correct, or is there another specific word for what I'm looking for?
Sep 18 '08 #1
  • viewed: 1027
Share:
1 Reply
jkmyoung
Expert 2GB
Finally managed to find a solution:
Expand|Select|Wrap|Line Numbers
  1. Declare @TMP TABLE
  2. (
  3. field1 Int
  4. field2 Int
  5. )
  6.  
  7. INSERT INTO @TMP
  8. SELECT id, otherVar
  9. from ....blah join blah join blah
  10. where bar = foo
  11.  
  12.  
  13. Declare @RC = int
  14. Declare @f1 = int
  15. Declare @f2 = int
  16. declare cur_1 cursor for select field1, field2 from @TMP
  17.  
  18. -- like a do-while
  19. fetch next from cur_1 into @f1, @f2;
  20. while @@FETCH_STATUS = 0
  21. begin
  22.     Exec @RC = [Process].[dbo].[usp.storedProc1]   @f1, @f2
  23.     fetch next from cur_1 into @f1, @f2;
  24. end 
  25. close cur_1;
  26.  
Sep 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Sam | last post: by
4 posts views Thread by D. Shane Fowlkes | last post: by
73 posts views Thread by Claudio Grondi | last post: by
7 posts views Thread by Jerry | last post: by
22 posts views Thread by silversurfer2025 | last post: by
1 post views Thread by pantagruel | last post: by
blyxx86
reply views Thread by blyxx86 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.