Please help me....Though this might sound silly

  • I am trying to update a table through cursor something like below based on data type

    DECLARE @b-2 nvarchar(255)

    DECLARE list cursor for

    select Column_Name

    FROM information_schema.columns

    WHERE table_schema = 'dbo'

    AND table_name = 'xyz'

    and data_type = 'some datatype'

    OPEN List

    FETCH NEXT FROM List

    INTO @b-2

    WHILE @@FETCH_STATUS = 0

    BEGIN

    print @b-2 -- using for checking whether cursor works or not

    print @@SERVERNAME -- using for checking whether cursor works or not

    UPDATE xyz

    SET @b-2 = ''

    FETCH NEXT FROM list INTO @b-2

    END

    CLOSE list

    DEALLOCATE list

    when i ran this it says the rows are being effected. But when i do a select * from the table it does not have any updation.

    plzz help me

  • UPDATE xyz

    SET @b-2 = ''

    You're updating a variable there... not a column. You also have no criteria in the UPDATE so you'll be updating the whole table. Lookup how to use cursors in Books Online. But before you do that, I recommend you lookup UPDATE so you can see that you don't actually need the slothfullness of a cursor here.

    [Edit] I see what you're trying to do and it won't work. You cannot update columns in such a fashion unless you use dynamic SQL.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you!! Jeff

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

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