I've been beating my head over this brick wall all morning. This is a very simplified example of the error(s) I've been receiving. I am using SSMS.
我整个早上一直在这个砖墙上敲我的头。这是我收到的错误的一个非常简单的例子。我正在使用SSMS。
DECLARE @myid nvarchar(10) = '5'
DECLARE @sql nvarchar(2048) = 'SELECT id FROM Applications A WHERE A.id=@myid'
EXECUTE sp_executesql @sql, @myid=@myid
Errors:
错误:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '5'. Msg 137, Level 15, State 2, Line 1 Must declare the scalar variable "@myid".
消息102,级别15,状态1,行1'5'附近的语法不正确。消息137,级别15,状态2,行1必须声明标量变量“@myid”。
Why am I getting the syntax, and scalar errors? @myid is defined, right?
为什么我会得到语法和标量错误? @myid是定义的,对吗?
1 个解决方案
#1
4
sp_executesql
needs to receive a definition of the parameters, which you are missing. So, in your case, you should use:
sp_executesql需要接收缺少的参数定义。所以,在你的情况下,你应该使用:
DECLARE @myid nvarchar(10) = '5';
DECLARE @sql nvarchar(2048) = 'SELECT id FROM Applications A WHERE A.id=@myid';
EXECUTE sp_executesql @sql, N'@myid nvarchar(10)', @myid=@myid;
#1
4
sp_executesql
needs to receive a definition of the parameters, which you are missing. So, in your case, you should use:
sp_executesql需要接收缺少的参数定义。所以,在你的情况下,你应该使用:
DECLARE @myid nvarchar(10) = '5';
DECLARE @sql nvarchar(2048) = 'SELECT id FROM Applications A WHERE A.id=@myid';
EXECUTE sp_executesql @sql, N'@myid nvarchar(10)', @myid=@myid;