如何在动态sql语句中使用表变量?
在我的存储过程中,我在过程顶部声明了两个表变量。现在我尝试在动态 sql 语句中使用该表变量,但在执行该过程时收到此错误。我正在使用 Sql Server 2008。
这就是我的查询的样子,
set @col_name = 'Assoc_Item_'
+ Convert(nvarchar(2), @curr_row1);
set @sqlstat = 'update @RelPro set '
+ @col_name
+ ' = (Select relsku From @TSku Where tid = '
+ Convert(nvarchar(2), @curr_row1) + ') Where RowID = '
+ Convert(nvarchar(2), @curr_row);
Exec(@sqlstat);
我收到以下错误,
必须声明表变量“@RelPro”。 必须声明表变量“@TSku”。
我尝试将表置于动态查询的字符串块之外,但无济于事。
In my stored procedure I declared two table variables on top of my procedure. Now I am trying to use that table variable within a dynamic sql statement but I get this error at the time of execution of that procedure. I am using Sql Server 2008.
This is how my query looks like,
set @col_name = 'Assoc_Item_'
+ Convert(nvarchar(2), @curr_row1);
set @sqlstat = 'update @RelPro set '
+ @col_name
+ ' = (Select relsku From @TSku Where tid = '
+ Convert(nvarchar(2), @curr_row1) + ') Where RowID = '
+ Convert(nvarchar(2), @curr_row);
Exec(@sqlstat);
And I get the following errors,
Must declare the table variable "@RelPro".
Must declare the table variable "@TSku".
I have tried to take the table outside of the string block of dynamic query but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 SQL Server 2008+ 上,只要您不需要更新表本身中的值,就可以使用表值参数将表变量传递到动态 SQL 语句。
因此,根据您发布的代码,您可以将此方法用于
@TSku
,但不能用于@RelPro
下面的示例语法。
包含 physloc 列只是为了证明子作用域中引用的表变量绝对与外部作用域相同,而不是副本。
On SQL Server 2008+ it is possible to use Table Valued Parameters to pass in a table variable to a dynamic SQL statement as long as you don't need to update the values in the table itself.
So from the code you posted you could use this approach for
@TSku
but not for@RelPro
Example syntax below.
The
physloc
column is included just to demonstrate that the table variable referenced in the child scope is definitely the same one as the outer scope rather than a copy.您的 EXEC 在不同的上下文中执行,因此它不知道在原始上下文中声明的任何变量。您应该能够使用临时表而不是表变量,如下面的简单演示所示。
Your EXEC executes in a different context, therefore it is not aware of any variables that have been declared in your original context. You should be able to use a temp table instead of a table variable as shown in the simple demo below.
您不必使用动态 SQL
You don't have to use dynamic SQL
您不能这样做,因为表变量超出了范围。
您必须在动态 SQL 语句中声明表变量或创建临时表。
我建议您阅读这篇关于动态 SQL 的优秀文章。
http://www.sommarskog.se/dynamic_sql.html
You can't do this because the table variables are out of scope.
You would have to declare the table variable inside the dynamic SQL statement or create temporary tables.
I would suggest you read this excellent article on dynamic SQL.
http://www.sommarskog.se/dynamic_sql.html
好吧,我找到了方法,并想与可能遇到同样问题的人分享。
让我从我一直面临的问题开始,
我一直在尝试执行一个动态 Sql 语句,该语句使用我在存储过程顶部声明的两个临时表,但是因为该动态 sql 语句创建了一个新范围,所以我无法不要使用临时表。
解决方案:
我只是将它们更改为全局临时变量并且它们起作用了。
在下面找到我的存储过程。
作为
开始
-- 添加 SET NOCOUNT ON 以防止额外的结果集
-- 干扰 SELECT 语句。
设置不计数;
结尾
去
Well, I figured out the way and thought to share with the people out there who might run into the same problem.
Let me start with the problem I had been facing,
I had been trying to execute a Dynamic Sql Statement that used two temporary tables I declared at the top of my stored procedure, but because that dynamic sql statment created a new scope, I couldn't use the temporary tables.
Solution:
I simply changed them to Global Temporary Variables and they worked.
Find my stored procedure underneath.
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
END
GO
我认为这是不可能的(尽管请参阅下面的更新);据我所知,表变量仅存在于声明它的范围内。但是,您可以使用临时表(使用
创建表
语法并在表名前添加 # 符号),并且可以在以下范围内访问该临时表:创建它以及动态语句的范围。更新:请参阅 Martin Smith 的答案,了解如何使用表值参数将表变量传递到动态 SQL 语句。另请注意提到的限制:表值参数是只读的。
I don't think that is possible (though refer to the update below); as far as I know a table variable only exists within the scope that declared it. You can, however, use a temp table (use the
create table
syntax and prefix your table name with the # symbol), and that will be accessible within both the scope that creates it and the scope of your dynamic statement.UPDATE: Refer to Martin Smith's answer for how to use a table-valued parameter to pass a table variable in to a dynamic SQL statement. Also note the limitation mentioned: table-valued parameters are read-only.
以下是使用动态 T-SQL 查询的示例,如果您有多个返回值列(请注意动态表名称),则提取结果:
Here is an example of using a dynamic T-SQL query and then extracting the results should you have more than one column of returned values (notice the dynamic table name):
使用 Temp 表解决了问题,但我在使用 Exec 时遇到了问题,因此我采用了以下使用 sp_executesql 的解决方案:
Using Temp table solves the problem but I ran into issues using Exec so I went with the following solution of using sp_executesql: