2012-03-13 18 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] 

注:これは私が実行したいクエリの一部でしかありません、私は、テーブル名とクエリで変数をsubsituteする方法を見つけ出すしたいと思います。また、私はこれが良い形ではないことを知っていますが、私はこのようにする必要があります。ここ

更新作業コード:

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

をホープ、sumscore、カウント数として合計(quality_score)を_key_out選択(*)を再計算として、(sum(quality_score)/ count (*))aveとして into tempga0 fro 'が長すぎます。最大長は128です。コメントにコードを投稿することはできませんので、私が試しているコードを表示するように私の質問を更新しました – Dorf

+0

ああ、私はちょうど二重引用符を一重引用符に変更する必要があることが判明しました。私はちょうど 'OBJECT_ID IF(' tempga0 ' )IS NOT NULL DROP TABLE tempga0 'がテーブルが壊れていないかどうかを確認するために私の質問で作業コードを更新してください、感謝Jaques – Dorf

関連する問題