Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    45

    Unhappy Unanswered: UPdate a variable Column

    How can i do something like that with out using the building of a string.

    -- table test ( id, col1, col2)

    DECLARE @colname varchar(20),
    @colvalue varchar(20)

    SET @colname = 'col1'
    SET @colvalue = 'test1'

    INsert into test(id, @colname) Values(1, @colvalue)


    Thanks
    Franky
    FBoucheros@hotmail.com

  2. #2
    Join Date
    Dec 2003
    Location
    Toronto
    Posts
    15

    using syscolumns table for "parametrized" insert

    I guess your problem is that you want to programmatically control which column will suffer a certain insert.

    The following example shows you how to use SYSCOLUMNS in order to retrieve and use the name of a certain column.

    I hope it helps,

    cezarm

    DECLARE
    @colname varchar(20),
    @colvalue varchar(20),
    @colid int -- this variable is keeping the colid from syscolumns

    set @colname = COL_NAME(OBJECT_ID('test'), 2)
    SET @colvalue = 'test1'

    exec
    ('INsert into test(id,' + @colname + ') Values(1, ''' + @colvalue + ''')')
    --------------------------------------------------------------------------

  3. #3
    Join Date
    Feb 2002
    Posts
    2,232
    Sorry - no can do. Check out sp_executesql in bol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •