I Had Update Command. Just Like this.
Update DispatchExceptions
SET TYPE='MWM'
WHERE ID=9801246
I want to fire same command using dynamic sql.
DECLARE @ColumnName varchar(20)=N'TYPE'
DECLARE @ColumnValue char(3)='MWM'
DECLARE @ID INTEGER = 9801246
declare @Query nvarchar(max)
SET @Query = 'Update DispatchExceptions SET '+ QUOTENAME(@ColumnName) + '''=' + @ColumnValue + '''WHERE ID =' + @ID +''
EXEC (@Query)
But it show following error.
Conversion failed when converting the nvarchar value 'Update DispatchExceptions SET [TYPE]'=MWM'WHERE ID =' to data type int.
How can I use dynamic sql in the situation. Any suggestion.
[TYPE]='MWM'not[TYPE]'=MWM'. And something similar for your ID.QUOTENAMEon@ColumnName, but@ColumnValueand@IDshould be parametrised, not concatenated.