Using variables in CURSOR declarations

  • I need to make a dynamic SQL query passed as a select

    statement in cursor mode in MSSQl.

    e.g

    DECLARE cr CURSOR

    FOR

    EXEC('SELECT * FROM' + @tname)

    As you can see I do not know the table name as the time

    of creating this cursor(in an Stored Proc) which shall be

    passed as a parameter.

    Please suggest an alternative or any way to make this

    work.

    Any help would be appreciated

    Thanks in Adv.

  • You will need to build the whole process dynamically including the cursor work.

  • Can you give an example for this ?

  • DECLARE @sql nvarchar(100)

    SET @sql = 'DECLARE cr CURSOR FOR SELECT * FROM ' + @tname

    EXEC (@sql)

    OPEN cr

    CLOSE cr

    DEALLOCATE cr

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Hurray!!!!

    It works.....

    Thanks a million

Viewing 5 posts - 1 through 5 (of 5 total)

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