2016-02-12 72 views
0

我需要從具有用戶定義表類型的存儲過程對錶進行批量更新。更新導致表鎖定,我想減輕這一點。我想批量他們的1000更新的代碼看起來如下SQL Server批量更新,用戶定義的表類型

CREATE PROCEDURE [dbo].[updatestuff] 
    @update1 AS udtupdate1 READONLY , 
    @update2 AS udtupdate2 READONLY , 
    @time DATETIME 
AS 
    BEGIN  

    SET NOCOUNT ON   

-- uses udtupdate1  
     UPDATE lms 
     SET  lms.col1 = lsp.col1 , 
       lms.TimeStamp = @time 
     FROM dbo.tlivetable1 lms 
       INNER JOIN @update1 lsp ON lms.livetable1Id = lsp.colId  


     UPDATE ms 
     SET  ms.col1 = lsp.col1 , 
       ms.TimeStamp = @time 
     FROM dbo.tmrks ms 
       INNER JOIN @update1 lsp ON ms.mrksId = lsp.Colid  

-- uses udtupdate2 

     UPDATE mms 
     SET  mms.col2 = msp.col2 , 
       mms.Date = @time 
     FROM dbo.tMMSel mms 
       INNER JOIN @update2 msp ON msp.col1Id = mms.MMId 
       AND msp.col2Id = mms.MMSId 

END  

任何幫助將非常感激

回答

0

我沒有測試過這個,因爲我沒有任何樣本數據,但這應該這樣做。:

CREATE PROCEDURE [dbo].[updatestuff] 
    @update1 AS udtupdate1 READONLY , 
    @update2 AS udtupdate2 READONLY , 
    @time DATETIME 
AS 
    BEGIN  

    SET NOCOUNT ON   

    WHILE (1 = 1) 
    BEGIN 
     BEGIN TRANSACTION 

     UPDATE top (1000) lms 
      SET  lms.col1 = lsp.col1 , 
       lms.TimeStamp = @time 
      FROM dbo.tlivetable1 lms 
       INNER JOIN @update1 lsp ON lms.livetable1Id = lsp.colId 

     IF @@ROWCOUNT = 0 -- terminating condition; 
     BEGIN 
      COMMIT TRANSACTION 
      BREAK 
     END 

     COMMIT TRANSACTION 
    --WAITFOR DELAY '00:00:01'; --<-- you can uncomment wait for delay to increase the delay between batches 
    END 

    WHILE (1 = 1) 
    BEGIN 
     BEGIN TRANSACTION 

      UPDATE top (1000) ms 
      SET  ms.col1 = lsp.col1 , 
       ms.TimeStamp = @time 
      FROM dbo.tmrks ms 
       INNER JOIN @update1 lsp ON ms.mrksId = lsp.Colid  

     IF @@ROWCOUNT = 0 -- terminating condition; 
     BEGIN 
      COMMIT TRANSACTION 
      BREAK 
     END 

     COMMIT TRANSACTION 
    --WAITFOR DELAY '00:00:01'; --<-- you can uncomment wait for delay to increase the delay between batches 
    END 

    WHILE (1 = 1) 
    BEGIN 
     BEGIN TRANSACTION 

      UPDATE top (1000) mms 
      SET  mms.col2 = msp.col2 , 
       mms.Date = @time 
      FROM dbo.tMMSel mms 
       INNER JOIN @update2 msp ON msp.col1Id = mms.MMId 
       AND msp.col2Id = mms.MMSId 

     IF @@ROWCOUNT = 0 -- terminating condition; 
     BEGIN 
      COMMIT TRANSACTION 
      BREAK 
     END 

     COMMIT TRANSACTION 
    --WAITFOR DELAY '00:00:01'; --<-- you can uncomment wait for delay to increase the delay between batches 
    END 

END 
+0

Thankyou爲此,我會測試並讓你發佈 – Jdem