SQL 为什么我得到“程序需要'ntext/nchar/nvarchar'类型的参数'@statement'。” 当我尝试使用 sp_executesql 时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2743890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'." when I try to use sp_executesql?
提问by Manoj Wadhwani
Why do I get this error
为什么我收到这个错误
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
when I try to use sp_executesql?
当我尝试使用 sp_executesql 时?
回答by AdaTheDev
Sounds like you're calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR.
听起来您在调用 sp_executesql 时需要使用 VARCHAR 语句,但需要使用 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
回答by Daniel Renshaw
The solution is to put an N in front of both the type and the SQL string to indicate it is a double-byte character string:
解决办法是在类型和SQL字符串前都加一个N,表示它是一个双字节字符串:
DECLARE @SQL NVARCHAR(100)
SET @SQL = N'SELECT TOP 1 * FROM sys.tables'
EXECUTE sp_executesql @SQL
回答by simaglei
I had missed another tiny detail: I forgot the brackets "(100)" behind NVARCHAR.
我错过了另一个小细节:我忘记了 NVARCHAR 后面的括号“(100)”。