2012-03-13 77 views
1

我想要做的是在多个表上多次运行一个查询,所以我在这里是一个表名称循环表@tablename到名称的表我希望在每个迭代上运行查询。使用变量名在多个表上运行查询

正如你可以看到下面@tablename是我想运行查询的表的名称,但我如何使用@tablename作为表名运行这些查询?

CREATE TABLE [BusinessListings].[dbo].[temptablenames] 
(id int, 
name nvarchar(50), 
) 

INSERT INTO [BusinessListings].[dbo].[temptablenames] (id, name) 
VALUES 
(1,'MongoOrganisationsACT1'), 
(2,'MongoOrganisationsNSW1'), 
(3,'MongoOrganisationsNT1'), 
(4,'MongoOrganisationsQLD1'), 
(5,'MongoOrganisationsSA1'), 
(6,'MongoOrganisationsTAS1'), 
(7,'MongoOrganisationsVIC1'), 
(8,'MongoOrganisationsWA1'); 

DECLARE @tablename sysname, 
@id int 
SET @id = 1 
WHILE (@id < 9) 
BEGIN 
select @tablename = name from temptablenames where id = @id 

select @tablename 


     select _key_out, sum(quality_score) as sumscore, count(*) as reccount, (sum(quality_score)/count(*)) as ave 
     into tempga0 
     from @tablename 
     group by _key_out 

     select _key_out, count(*) as reccount 
     into tempga3 
     from @tablename 
     where dedupe_result is null 
     group by _key_out 
     having count(*)>1 

     select a._key_out, max(quality_score) as maxdedupetotalscore 
     into tempga4 
     from 
     @tablename a 
     join 
     tempga3 b 
     on a._key_out = B._key_out 
     --where isdeleted is null 
     group by a._key_out 

     --- keep records 
     update @tablename 
     set dedupe_result = 'Keep' 
     from 
     @tablename a 
     join 
     tempga4 b 
     on a._key_out = B._key_out 
     where a.quality_score = b.maxdedupetotalscore 
     --and isdeleted is null 
     and dedupe_result is null 

SET @id = @id + 1 
END 
GO 

DROP TABLE [BusinessListings].[dbo].[temptablenames] 

注意:这仅仅是我想要运行的查询的一部分,我只是想弄清楚如何替补多变量查询作为表名。此外,我知道这不是一个好的形式,但有一个原因,我需要这样做。

更新工作代码在这里:

DECLARE @tablename nvarchar(30), 
@id int, 
@SQLStr nvarchar(1000) 
SET @id = 1 
WHILE (@id < 9) 
BEGIN 
select @tablename = name from temptablenames where id = @id 

IF OBJECT_ID('tempga0') IS NOT NULL 
DROP TABLE tempga0 

    set @SQLStr = 'select _key_out, sum(quality_score) as sumscore, count(*) as reccount, (sum(quality_score)/count(*)) as ave 
into tempga0 
from ' + @tablename + ' group by _key_out' 

    exec(@SQLStr) 


SET @id = @id + 1 
END 
GO 

回答

1

使用exec命令。在一个变量中写下你的查询并执行它

Declare @SQLStr = 'Select * into X from ' + @tablename 
exec(@SQLStr) 

你只需要小心。我看到你正在使用语句。你将不得不检查表是否已经存在,因为你会得到一个异常。你将需要删除的表,或者更好的办法是做这一点,你开始你的循环之前:

CREATE TABLE tempga0 (
_key_out int, 
sumscore numeric(18,9), 
reccount int, 
ave numeric(18,9)) 

--rest of the tables to be created here... 

创建所有的表,当您启动While循环添加

WHILE (@id < 9)   
BEGIN  
    TRUNCATE TABLE tempga0 
    --truncate the rest of the tables 

    --Do the rest of your stuff here 
END 

希望它可以帮助

+0

我不断收到一个错误,当我尝试这个我只是想获得第一个查询在这里工作是错误 “是开头的标识符”选择_key_out,总和(quality_score)作为sumscore,计数(*)作为reccount,(sum(quality_score)/ count (*))as ave into tempga0 fro'太长。最大长度是128。'我不能在评论中发布代码,所以我更新了我的问题,以显示代码我试图 – Dorf 2012-03-13 06:28:20

+0

啊我得到它的工作原来我只需要改变双引号单引号,我只是使用'IF OBJECT_ID('tempga0' )不是NULL DROP TABLE tempga0'检查表是否存在不正确更新我的问题中的工作代码,谢谢Jaques – Dorf 2012-03-13 06:40:41

相关问题