简体   繁体   English

T-SQL:使用“来自变量”查询更新临时表

[英]T-SQL: Update temp table using “from variable” query

I have a linked server connecting to a Lotus notes database as the source. 我有一个链接服务器,连接到Lotus Notes数据库作为源。 The destination will be an MS SQL database. 目标将是MS SQL数据库。

I have two temp tables. 我有两个临时表。 The first temp table is pulling in the table names from a linked server. 第一个临时表从链接服务器中提取表名。 From there, I want to do a record count for each table and store that value into the second temp table beside the table name. 从那里,我想为每个表进行记录计数,并将该值存储到表名旁边的第二个临时表中。

I am having trouble trying to run a loop or cursor for each table name and then updating the second temp table with the record count for each table name. 我在尝试为每个表名运行循环或游标,然后用每个表名的记录计数更新第二个临时表时遇到麻烦。

Right now I am getting an error "Incorrect syntax near 'Execute'". 现在,我收到一个错误“'Execute'附近的语法不正确”。 SET record_count = Execute(@sqlCommand)

Declare @DB_tables table (
table_cat varchar(1500),
table_schem varchar(1500),
table_name varchar(1500),
table_type varchar(1500),
remarks varchar(1500)
)


Declare @temp_table table (
table_name varchar(1500),
record_count varchar(255),
drop_script varchar(1500),
update_script varchar(1500)
)

--Load Initial data from linked server database
insert into @DB_Tables
exec sp_tables_ex [LINKED_SERVER_DB]

--Load table name from Stored Procedure
INSERT INTO @temp_table (table_name)
SELECT table_name from @DB_Tables 

--select * from @temp_table




--Variable to hold each table name in a loop or cursor
declare @tbl_name varchar(1500)
--declare @sqlCommand varchar(1500)


declare cur cursor for select table_name from @DB_Tables
Open cur

--Loop through each table name from the first temp table
--then update the second temp table (@temp_table) with the record count
FETCH NEXT FROM cur into @tbl_name

While @@FETCH_STATUS = 0 BEGIN

declare @sqlCommand varchar(1500)
--query used to get the record count from the frist temp table (@DB_tables)
SET @sqlCommand = 'select count(*) from '+@tbl_name

UPDATE @temp_table

SET record_count = Execute(@sqlCommand)

END
CLOSE cur
Deallocate cur



select * from @temp_table

It's not easy to use table variables with execute because the dynamic SQL is executed in the different context and doesn't see the variable and you can't assign results from execute that way. 将表变量与execute一起使用并不容易,因为动态SQL是在不同的上下文中执行的,并且看不到该变量,并且您不能以这种方式分配结果。

You can either insert the results into a table variable with this syntax: 您可以使用以下语法将结果插入表变量中:

insert into @temp_table 
execute ('select ' + @tbl_name + ', count(*) from ' + @tbl_name ...)

Or use temp. 或使用温度。 tables, since then you can refer them also inside the dynamic SQL, so you could do something like: 表,此后您还可以在动态SQL中引用它们,因此您可以执行以下操作:

create table #temp_table  (
table_name varchar(1500),
record_count varchar(255),
drop_script varchar(1500),
update_script varchar(1500)
)
...
Execute('update #temp_table set record_count = (select count(*) from '
        +@tbl_name+') where table_name = '''+@tbl_name+''')

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

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