Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

T-SQL Cursor Question

Status
Not open for further replies.

Jorgandr

Programmer
May 10, 2002
58
0
0
US
Is there a way to use a variable in the select_statement part of the DECLARE CURSOR?

For instance I would like to do the following

SELECT @SQL = 'SELECT [' + @FieldName + '] FROM [QueryName]'

DECLARE CURSOR_1 CURSOR FOR @SQL

However this does not seem to work. Is there any way to do this? Any help would be greatly appreciated. Thank you.
 
First using dynamic SQl is a bad idea (This is what you are asking to do) and second do not use a cursor! If you are doing an insert, update or delete a cursor is easily turned into a set-based command that is both easier to read and maintain and way faster (sometimes hours can convert to seconds or even milliseconds)



"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top