Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can a CTE be used instead of a cursor/while loop?

Author  Topic 

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-09-26 : 10:47:01
I need to take a result set, and for each row, call a number of stored procedures. Can this be accomplished with a CTE instead? I'm thinking no....

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-26 : 10:52:12
No. CTE still returns a resultset.
However, instead of using CURSOR you can use WHILE.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-26 : 10:53:13
The fastest way to do it, is to insert the resultset into a temporary table with an identity column.
Then do a WHILE for each record and call the SP.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-26 : 10:57:59
Or undocumented xp_execresultset ?
Go to Top of Page

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-09-26 : 11:03:20
quote:
Originally posted by Peso

The fastest way to do it, is to insert the resultset into a temporary table with an identity column.
Then do a WHILE for each record and call the SP.



E 12°55'05.25"
N 56°04'39.16"




Yeah, I know. However, you'd probably want to use a table variable instead of a temp table.
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2007-09-26 : 11:04:56
Depends on the table...
Go to Top of Page
   

- Advertisement -