Question: While running this query

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

```

I get an error:

> 
> Incorrect syntax near 'month'.
> 
> 
> 

The table `p_leave_allocation_14122015` has columns like `month1`, `month2`, `month3`,**.....**

In my update query what I want is `month` will be hardcoded and the no of month will be dynamic based on some conditions which I have written.

But when I tried with the above query I got the mentioned error.

I am using SQL Server 2005.

Kindly suggest

Answer: 
*You should try something like below*,

```
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'update p_leave_allocation_14122015 
           SET MONTH'+CAST(@actualMonth AS VARCHAR(2)) +' = ' + CAST(@actualleavedays AS VARCHAR(10))+
           ' WHERE YEAR = '+ CAST(@actualYear AS VARCHAR(4))+
           ' AND emp_card_no = '+CAST(@emp_card_no AS VARCHAR(100)) +'';

PRINT @sql --Note: Check the query first before updating.
EXEC SP_EXECUTESQL @sql;

```
You need to use `dynamic query` to do this 

```
DECLARE @sql NVARCHAR(max) = '' 

SET @sql = 'update p_leave_allocation_14122015       set month' 
           + Cast(@actualMonth AS VARCHAR(10)) + ' =  ' 
           + Cast(@actualleavedays AS VARCHAR(10)) 
           + ' where year = @actualYear  and emp_card_no =  @emp_card_no ' 

EXEC Sp_executesql 
  @sql, 
  N'@actualYear int,@emp_card_no int', 
  @actualYear, 
  @emp_card_no 

```

Note: If `@actualMonth` or `@actualleavedays` is of `integer` type then you need to cast it to `varchar` to work in dynamic query