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 2000 Forums
 SQL Server Development (2000)
 Building Dynamic Cursor SQL Statements

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-25 : 08:58:57
Ken writes "Is it possible to build a dynamic SQL statement and use it in the creation of a cursor. Based on the manual, it doesn't appear that this can be done. I need to use a cursor so I can do processing on each record in the result.

SET @SQL = 'SELECT ' + @SelectList + ' FROM ' + @TableName
DECLARE curs CURSOR FOR @SQL

If this cannot be done, is there any alternative way to iterate over a dynamically built query and do processing on each row of the result set in a SQL Server stored procedure?"

andre
Constraint Violating Yak Guru

259 Posts

Posted - 2002-02-25 : 09:54:04
An alternative is to use a temporary table. What kind of "processing" are you doing on each row? You might be surprised at what you can do with set-based processing using T-SQL. Set-based processing is faster than doing an operation row-by-row.

Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2002-02-25 : 14:38:01
You can build a cursor thru a dynamic sql statement. If this a process which you intend to run frequently, you should try both this and a set based solution to see which runs faster. Although the set based solution usually runs faster and is the better solution, contrary to what you may read, it is not the 100% guaranteed best solution for every problem.

Access is a virus!
Go to Top of Page
   

- Advertisement -