Fetch next question

  • I'm declaring and opening a cusor that consists of all columns from a given table. This is a fairly wide table, with about 30-35 columns.

    When doing a fetch next... into, do I have to declare a variable for every column in the table, or is there a mechanism that allows one variable to represent all columns?

    The following code snippet shows what I'm trying to accomplish:

    DECLARE curTemp CURSOR FAST_FORWARD FOR SELECT * FROM PTT

    OPEN curTemp

    FETCH NEXT FROM curTemp INTO @cPID, @cGID, ... (variables representing each columns)

    OR

    FETCH NEXT FROM curTemp INTO @SomeVarType (a variable that represents all columns)

  • Yes, unfortunately you do have to declare a variable for each column. Generally you want to make sure that you aren't pulling columns that you aren't going to use.

    If you are actually going to be using all of those columns probably the easiest method I've found to script the declares is to do the following:

    In SSMS or your favorite scripting utility, script out the table.

    Copy out the column definitions.

    For each line delete everything past the data type

    Then for each line past the word DECLARE at the beginning.

    Its not great but its certainly a lot easier than typing the whole thing out.

    Kenneth

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • Kenneth - Thanks for your reply and shortcut tip.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply