2012-03-13 27 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