为什么我要得到" Procedure expect参数'@statement of type 'ntext/nchar/nvarchar "“当我尝试使用sp_executesql时?”

时间:2021-02-09 01:51:22

Why do I get this error

为什么会出现这个错误

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

when I try to use sp_executesql?

当我尝试使用sp_executesql时?

2 个解决方案

#1


166  

Sounds like you're calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR.

听起来好像您正在用VARCHAR语句调用sp_executesql,而它需要是NVARCHAR。

e.g. This will give the error because @SQL needs to be NVARCHAR

这将会产生错误,因为@SQL需要是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

#2


12  

The solution is to put an N in front of both he type and the SQL string to indicate it is a double-byte character string:

解决方案是在he类型和SQL字符串前面加上一个N,表示它是双字节字符串:

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

#1


166  

Sounds like you're calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR.

听起来好像您正在用VARCHAR语句调用sp_executesql,而它需要是NVARCHAR。

e.g. This will give the error because @SQL needs to be NVARCHAR

这将会产生错误,因为@SQL需要是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

#2


12  

The solution is to put an N in front of both he type and the SQL string to indicate it is a double-byte character string:

解决方案是在he类型和SQL字符串前面加上一个N,表示它是双字节字符串:

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