循环删除SP

时间:2017-07-05 08:31:15

标签: tsql stored-procedures while-loop sql-server-2012

我需要一些帮助尝试为此SP创建一些处理我正在创建当前看起来像这样的...

 alter PROCEDURE ArchiveCampaignData 

    @CampaignId INT,
    @DeleteBatchOf INT

     AS


BEGIN

BEGIN   

    DECLARE @Rowcount INT = 1

    WHILE @Rowcount > 0

    DELETE TOP (@DeleteBatchOf)FROM dbo.u_activity
    OUTPUT DELETED.* INTO u_activity_archive
    WHERE campaignId = @CampaignId

    SET @Rowcount = @@ROWCOUNT
END


BEGIN   

    DECLARE @Rowcount2 INT = 1
    WHILE @Rowcount2 > 0

        DELETE TOP (@DeleteBatchOf) C
        OUTPUT DELETED.* INTO dbo.u_contact_archive
        FROM dbo.U_contact AS C
             INNER JOIN dbo.U_Campaigncontact AS CON WITH(NOLOCK)ON C.id = CON.contactId
        WHERE CON.campaignId = @CampaignId

        SET @Rowcount2 = @@ROWCOUNT

END

BEGIN   

    DECLARE @Rowcount3 INT = 1
    WHILE @Rowcount3 > 0

    DELETE TOP (@DeleteBatchOf) FROM u_campaignContact
    OUTPUT Deleted.* INTO u_campaignContact_archive
    WHERE campaignId = @CampaignId

    SET @Rowcount3 = @@ROWCOUNT

END


BEGIN   

    DECLARE @Rowcount4 INT = 1
    WHILE @Rowcount4 > 0

        DELETE TOP (@DeleteBatchOf) L
        OUTPUT DELETED.* INTO dbo.dxi_CDRLog_Archive
        FROM dbo.dxi_CDRLog AS L
             INNER JOIN dbo.u_campaignHdr AS H WITH(NOLOCK)ON L.qid = H.diallerCampaignId
        WHERE H.parentId = @CampaignId

        SET @Rowcount4 = @@ROWCOUNT

END


BEGIN   


        DECLARE @Rowcount5 INT = 1
        WHILE @Rowcount5 > 0

        DELETE TOP(@DeleteBatchOf) T
        OUTPUT Deleted.* INTO dxi_diallerStatsSmall_archive
        FROM dbo.dxi_diallerStatsSmall AS T
        INNER JOIN dbo.u_campaignHdr AS G WITH (NOLOCK) ON G.diallerCampaignId = T.campaign   
        WHERE G.id = @CampaignId

        SET @Rowcount5 = @@ROWCOUNT

 END

BEGIN

        DECLARE @Rowcount6 INT = 1
        WHILE @Rowcount6 > 0

        DELETE TOP(@DeleteBatchOf) FROM dbo.u_campaignHdr
        OUTPUT Deleted.* INTO u_campaignHdr_archive
        WHERE Id = @CampaignId OR parentId = @CampaignId

        SET @Rowcount6 = @@ROWCOUNT

END
END

目标是每个块在SP中循环以便一次运行,并且每个块完成后该块将停止其事务并且它将继续到下一个块以及打印'已完成&# 39;每次成功删除后。

任何建议/增强功能将不胜感激。

0 个答案:

没有答案