Why I get "The procedure expects a parameter" @statement "of type" ntext / nchar / nvarchar "." when i try to use sp_executesql?

Why am I getting this error

Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. 

when i try to use sp_executesql?

+88
sql sql-server tsql dynamic-sql
Apr 30 '10 at
source share
2 answers

It looks like you are calling sp_executesql with the VARCHAR statement when it should be NVARCHAR.

eg. This will give an error because @SQL must be NVARCHAR

 DECLARE @SQL VARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL 

So:

 DECLARE @SQL NVARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL 
+205
Apr 30 '10 at
source share

The solution is to put N in front of its type and SQL string to indicate that it is a double-byte character string:

 DECLARE @SQL NVARCHAR(100) SET @SQL = N'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL 
+20
Apr 30 '10 at 10:51
source share



All Articles