简体   繁体   English

动态SQL中的动态表

[英]dynamic table in dynamic SQL

i have a declared a table : 我有一个声明表:

DECLARE @V_TABLE (ROW_ID INT IDENTITY(1,1), CLIENTKEY, UNIQUEIDENTIFIER)

i have tried to use this table in a piece of dynamic sql 我试图在动态SQL中使用此表

SET @SQL = ' INSERT INTO #CLIENTTABLE ( CLIENTKEY )
              (SELECT CLIENTKEY FROM '+ @V_TABLE  +')'

the code keeps asking me to declare @v_table how can i use this table in the dynamic sql 代码不断要求我声明@v_table我如何在动态sql中使用此表

In your query @V_TABLE is a table variable and not a string variable containing the name of the table. 在您的查询中,@V_TABLE是一个表变量,而不是包含表名的字符串变量。 Hence, if the query is prepared like so, it should work. 因此,如果查询是这样准备的,它应该可以工作。

SET @SQL = ' INSERT INTO #CLIENTTABLE ( CLIENTKEY )
              (SELECT CLIENTKEY FROM @V_TABLE')'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM