SQL Server 表串連刪除

來源:互聯網
上載者:User

一哥們在做資料同步的時候寫了個遊標去迴圈更新資料庫裡面的表記錄:

-- Following is incremental update 

DECLARE @InterfaceCode_tmp varchar(12), @Affiliate_tmp char(3),@DealerNumber_tmp varchar(5),@DSPNumber_tmp varchar(12) 

--Process DealerAuthorization 

DECLARE dealerAuth_cursor CURSORFOR

SELECT InterfaceCode,Affiliate,DealerNumber FROM DealerAuthorization_tmp

OPEN dealerAuth_cursor

FETCHNEXTFROM dealerAuth_cursor INTO @InterfaceCode_tmp,@Affiliate_tmp,@DealerNumber_tmp

WHILE@@FETCH_STATUS= 0

BEGIN 

DELETE DealerAuthorization WHERE InterfaceCode=@InterfaceCode_tmp and Affiliate=@Affiliate_tmp and DealerNumber=@DealerNumber_tmp 

INSERT INTO DealerAuthorization (InterfaceCode, Affiliate, DealerNumber, isValid,ExpireDate)

SELECT InterfaceCode, Affiliate, DealerNumber, isValid, ExpireDate

FROM DealerAuthorization_tmp WHERE InterfaceCode=@InterfaceCode_tmp and Affiliate=@Affiliate_tmp and DealerNumber=@DealerNumber_tmp

FETCH NEXTF ROM dealerAuth_cursor INTO @InterfaceCode_tmp,@Affiliate_tmp,@DealerNumber_tmp

END

CLOSE dealerAuth_cursor

DEALLOCATE dealerAuth_cursor

尼瑪,我們資料庫有5萬多條資料,我不知道要執行多久。還好資料都不是很大,欄位也不多,他說只用一秒就完了。

想起以前寫過一個,幫他改了下

DELETE  FROM DealerAuthorization

FROM  DealerAuthorization a inner join DealerAuthorization_tmp b

ON  a.DealerNumber = b.DealerNumber and a.Affiliate = b.Affiliate and a.InterfaceCode = b.InterfaceCode

 

INSERT  INTO  DealerAuthorization(InterfaceCode, Affiliate, DealerNumber, isValid,ExpireDate)

SELECT  InterfaceCode, Affiliate, DealerNumber, isValid,ExpireDate

FROM  DealerAuthorization_tmp

看起來舒服多了。嘿嘿。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.