-2

2010年より前に本番データベースの特定のテーブルのデータをアーカイブデータベース(本番と同じ構造で作成)に移動するストアドプロシージャを作成する必要があります。私が使用した方法は、本番からアーカイブにデータをコピーし、アーカイブに存在する本番のデータを削除することです。コードは次のとおりです。

--## INSERT data from Production DB to Archive DB

    --tableMaster
    INSERT INTO [DB_Archive].[dbo].[tableMaster]
    select * from [DB_Production].[dbo].[tableMaster]
    where QuoDate < '2010-01-01 00:00:00.000'

    --tableDetail
    INSERT INTO [DB_Archive].[dbo].[tableDetail]
    select * from [DB_Production].[dbo].[tableDetail] tblDet
    where exists (select tblMaster.QuoNo,tblMaster.AgentCode from [DB_Archive].[dbo].[tableMaster] tblMaster
                                                    where tblDet.QuoNo = tblMaster.QuoNo)

    --tableSubDetail1
    INSERT INTO [DB_Archive].[dbo].[tableSubDetail1]
    select * from [DB_Production].[dbo].[tableSubDetail1] tblsub1
    where exists (select tblMaster.QuoNo,tblMaster.AgentCode from [DB_Archive].[dbo].[tableMaster] tblMaster 
                                                    where tblsub1.QuoNo = tblMaster.QuoNo)

    --tableSubDetail2
    INSERT INTO [DB_Archive].[dbo].[tableSubDetail2]
    select * from [DB_Production].[dbo].[tableSubDetail2] tblsub2
    where exists (select tblMaster.QuoNo,tblMaster.AgentCode from [DB_Archive].[dbo].[tableMaster] tblMaster 
                                                    where tblsub2.QuoNo = tblMaster.QuoNo)


    --## DELETE data from Production DB

    --tableMaster
    DELETE tblM FROM [DB_Archive].[dbo].[tableMaster] tblM
    Where exists (select bmas.QuoNo from [DB_Archive].[dbo].[tableMaster] bmas 
                                        where tblM.QuoNo = bmas.QuoNo)

    --tableDetail
    DELETE tblD FROM [DB_Archive].[dbo].[tableDetail] tblD
    Where exists (select bmas.QuoNo from [DB_Archive].[dbo].[tableDetail] bmas 
                                        where tblM.QuoNo = bmas.QuoNo)

    --tableSubDetail1
    DELETE tblS1 FROM [DB_Archive].[dbo].[tableSubDetail1] tblS1
    Where exists (select bmas.QuoNo from [DB_Archive].[dbo].[tableSubDetail1] bmas 
                                        where tblM.QuoNo = bmas.QuoNo)

    --tableSubDetail2
    DELETE tblS2 FROM [DB_Archive].[dbo].[tableSubDetail2] tblS2
    Where exists (select bmas.QuoNo from [DB_Archive].[dbo].[tableSubDetail2] bmas 
                                        where tblM.QuoNo = bmas.QuoNo)

上記は4つのテーブルのみで、同じスクリプトで30++のテーブルがあります。このコードをシンプルで短くする方法を教えてもらえますか? ありがとうございました

4

1 に答える 1