locked
Incorrect syntax near 'month'. RRS feed

  • Question

  • User-1506965535 posted

    while running the below query i get error as

    Incorrect syntax near 'month'.

    update p_leave_allocation_14122015 set 'month' + @actualMonth  = @actualleavedays
    where year = @actualYear  and emp_card_no =  @emp_card_no 

    what is wrong here ?

    Monday, December 21, 2015 10:28 AM

Answers

  • User954927490 posted

    Hi do like below

    declare @column_name as nvarchar(50)
    declare @columnvalue as nvarchar(50)
    declare @variable as nvarchar(50)
    declare @sql nvarchar (1000);
    
    set @variable='2'
    
    set @column_name='Month' +@variable
    set @columnvalue = 'updated val'
    set @sql = N'update TestMonth set ' + @column_name + '= '''+@columnvalue+'''' + ' where id=2';
    
    exec sp_executesql @sql;

    I have checked, this works fine

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 21, 2015 11:46 AM

All replies

  • User-1716253493 posted

    You want to update [month] field, the query should like

    update p_leave_allocation_14122015 set [month] = 

    Maybe

    update p_leave_allocation_14122015 set [month] = @actualMonth, actualleavedays = @actualleavedays
    where year = @actualYear  and emp_card_no =  @emp_card_no 

    Monday, December 21, 2015 10:38 AM
  • User-1506965535 posted

    No, its not like that,

    the full column contains a dynamic month, which can be month1, month2, month3,........

    so i want that dynamic name. 

    Monday, December 21, 2015 10:41 AM
  • User954927490 posted

    Hi do like below

    declare @column_name as nvarchar(50)
    declare @columnvalue as nvarchar(50)
    declare @variable as nvarchar(50)
    declare @sql nvarchar (1000);
    
    set @variable='2'
    
    set @column_name='Month' +@variable
    set @columnvalue = 'updated val'
    set @sql = N'update TestMonth set ' + @column_name + '= '''+@columnvalue+'''' + ' where id=2';
    
    exec sp_executesql @sql;

    I have checked, this works fine

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 21, 2015 11:46 AM