Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004

    Unanswered: Update statement with variables

    I'm trying to execute an update statement in a stored procedure that includes a variable for a column and a variable for a conditional constraint. For example I want to execute the following UPDATE, but using variables:

    Update table set flagcode = 'A' where Field1 < 100

    the field flagcode is a varchar and the field Field1 is an int
    This basically how I have my code set up:

    declare @flag as varchar(20)
    declare @lowrange as varchar(20)

    set @flag = 'A'
    set @thefield = 'Field1'

    exec('update table set Flagcode = ' + @flag + ' where '+@thefield+' < 100')

    I get the error:
    "Server: Msg 207, Level 16, State 3, Line 1
    Invalid column name 'A'."

    Please help T-SQL Guru's. You're my only hope.

  2. #2
    Join Date
    Mar 2004
    You need quotations marks around the string. To specify a quote within a string, use a 2 quotes.

    This should do it:
    exec('update table set Flagcode = ''' + @flag + ''' where '+@thefield+' < 100')

  3. #3
    Join Date
    Jun 2004
    That worked. Thanks.

Posting Permissions

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