zoukankan      html  css  js  c++  java
  • SQL Server 表连接删除

    一哥们在做数据同步的时候写了个游标去循环更新数据库里面的表记录:

    -- 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

    看起来舒服多了。嘿嘿。

  • 相关阅读:
    C++文件流类与文件流对象
    当java出现异常,应如何进行处理
    C语言函数的声明以及函数原型
    MySQL的create table as 与 like区别
    java中BigDecimal加减乘除基本用法
    项目小结
    自动化测试 如何快速提取Json数据
    Java Map 集合类在selenium自动化测试设计中的应用
    UFT对于PDF 文档的操作方法 VBS代码
    Selenium 自动化测试中对页面元素的value比较验证 java语言
  • 原文地址:https://www.cnblogs.com/JasonLiao/p/2528535.html
Copyright © 2011-2022 走看看