0

Below is the query causing error:

EXECUTE (' UPDATE facetswrk.dbo.ODS_SUBSC_PREM_REPORT ' + ' SET ' + @lcrcolumn_name + ' = ' + @lcrcolumn_total)
4
  • 3
    what are the values for @lcrcolumn_name and @lcrcolumn_total ? Commented Feb 5, 2016 at 11:05
  • if you are updating varchar column then syntex : EXECUTE (' UPDATE facetswrk.dbo.ODS_SUBSC_PREM_REPORT ' + ' SET ' + @lcrcolumn_name + ' = ' ' + @lcrcolumn_total+''') Commented Feb 5, 2016 at 11:09
  • Side note : use sp_executesql to reduce sql injection chance Commented Feb 5, 2016 at 11:14
  • sp_executesql by itself won't protect from SQL Injection if the statement is generated by string concatenation. Using parameterized queries will Commented Jun 21, 2016 at 14:48

2 Answers 2

1

Your syntax is ok, probably you have wrong valye for column name, or you need to cast @lcrcolumn_tot as nvarchar.

Give us the value for the variable, pr check by yourself with the flowing statement:

declare @lcrcolumn_name nvarchar(50) = 'blabla',
        @lcrcolumn_tot nvarchar(50) = 10
declare @sql nvarchar(4000);

set @sql = ' UPDATE facetswrk.dbo.ODS_SUBSC_PREM_REPORT SET ' + @lcrcolumn_name + ' = ' + @lcrcolumn_tot

print @sql
execute(@sql)
Sign up to request clarification or add additional context in comments.

Comments

0

Best is to print the dynamic sql before you execute it to understand what is causing the error, you may have some data value in @lcrcolumn_name and @lcrcolumn_total which may be creating the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.