kresa kresa - 4 days ago 5
SQL Question

How to write the following command in T-SQL

DECLARE @sqlCommand nvarchar(1000)

SET @sqlCommand = 'SELECT TOP 1
@isDeleted = IsDeleted,
@deletedDate = Deleted
FROM ' + @FullTableName +
'WHERE ID = ' + cast(@RowID as nvarchar(30)) + '';

EXECUTE sp_executesql @sqlCommand,
N'@isDeleted bit out N@deletedDate DATETIME out',
@isDeleted out, @deletedDate out


I get an error:


Msg 102, Level 15, State 1, Line 1

Incorrect syntax near 'N@deletedDate'


I am not sure how to write the last line.

It worked for
@isDeleted
but when I tried to add another variable
@deletedDate
, it stopped working.

Answer

There is an unnecessary N instead of comma in second parameter between the variable declaration. You need to add a single N before the second parameter to make the string of NVARCHAR type.

Try this

EXECUTE Sp_executesql
  @sqlCommand,
  N'@isDeleted bit out, @deletedDate DATETIME out',
  @isDeleted out,
  @deletedDate out 
Comments