Dynamic update help

  • I am running into a syntax error when running the follow code:

    declare @temp varchar(25)

    declare @impstore varchar (10)

    declare @sql varchar(1000)

    set @temp = 'Store_A'

    set @impstore = '18'

    exec ('update temp_data set month1_amount = select '

           + @temp + ' from sales where store = ' +@impstore)

    The error is Server: Msg 170, Level 15, State 1, Line 8

    Line 8: Incorrect syntax near ')'.

    I am new at sql server and am plugging along on this. Any help would be great.

  • Hi,

    The syntax error occurs because you need to put brackets around the select statement that generates the value.  Like this,

    exec ('update temp_data set month1_amount = (select '

           + @temp + ' from sales where store = ' + @impstore + ')')

    Hope that helps,

     

  • Thank you that worked great!

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

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