SQL Server Performance

Cursor - Changing name table

Discussion in 'T-SQL Performance Tuning for Developers' started by luma, Sep 12, 2005.

  1. luma New Member

    Hallo, is it possible write statement with some parameter in after definition of cursur ?
    I usually work with cursor, but in cycle i need to change name of table


    Declare au_cursor Cursor

    FOR

    SELECT id,tabs FROM k_Tabs
    Open au_cursor
    Fetch Next FROM au_cursor into @id, @tabs

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    Set @tmp= @tabs ' @tmp is name of table which i reed from another table and work with it

    Declare ns_cursor Cursor
    FOR

    set @sqlCommand='Select ns, price from '+@tmp
    execute(@sqlCommand)

    Open ns_cursor
    Fetch Next From ns_cursor into @ns,@cena

    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    .
    .
    .
    Fetch Next From zar_cursor into @program,@interval1,@interval2,@zlavaPerc
    END
    Close ns_cursor
    Deallocate ns_cursor
    .
    .
    .
    Fetch Next FROM au_cursor into @id, @tabDatabaza
    END

    Close au_cursor
    Deallocate au_cursor



    Error message

    Server: Msg 156, Level 15, State 1, Line 86
    Incorrect syntax near the keyword 'set'.

    Thanks, Lubo
  2. Madhivanan Moderator

    Why do you want to use Cursor?

    Replace these
    set @sqlCommand='Select ns, price from '+@tmp
    execute(@sqlCommand)

    by
    execute('Select ns, price from '+@tmp)


    Madhivanan

    Failing to plan is Planning to fail
  3. luma New Member

    When i am using

    execute('Select ns, price from '+@tmp)

    then is problem with executing of statement

    Server: Msg 156, Level 15, State 1, Line 90
    Incorrect syntax near the keyword 'execute'.
  4. Madhivanan Moderator

    Clearly tell us what you are trying to do
    I think there must be set based approach to do the same thing

    Madhivanan

    Failing to plan is Planning to fail

Share This Page