zoukankan      html  css  js  c++  java
  • 记录一下自己的游标小脚本

    ---游标循环遍历--
    begin
      declare @id int,@temp int,@error int
      set @error=0
      begin tran --申明事务
      --业务--
      update SmartPromoter set CustomerID=a.ID from SmartCustomer a,SmartPromoter b 
         where (a.Mobile=b.Mobile or a.MobileBackup=b.Mobile) and b.CustomerID is null and len(b.Mobile)=11
    
      declare promoter_cursor cursor for(select ID from SmartPromoter where CustomerID is null and len(Mobile)=11) --申明游标
       --打开游标--
      open promoter_cursor 
      --开始循环游标变量--
      fetch next from promoter_cursor into @temp
      while @@FETCH_STATUS=0 --返回被 FETCH语句执行的最后游标的状态--
        begin
          insert into SmartCustomer([Name],[Gender],[Mobile],[WeChat],[CreateTime],[ChannelID],[Remark],[CreateUserID],[Deposit],[Coupon],[Point],[CurrentExploitUserID],[CurrentManagerUserID],
          [VisitTimes],[ConsultTimes],[IsBlacklist],[IsGreylist],[HasComplain],[HasAppointment],[WechatBindTime],[MemberCategoryID],[CashCardTotalAmount]) 
          select Name,2,Mobile,Wechat,CreateTime,126,Remark,1,0,0,0,2,2,0,0,0,0,0,0,CreateTime,1,0 from SmartPromoter a where ID=@temp select @id = @@IDENTITY
          set @error= @error + @@ERROR --记录每次运行sql后是否正确,0正确
          update SmartPromoter set CustomerID=@id where ID=@temp
          set @error= @error + @@ERROR --记录每次运行sql后是否正确,0正确
            fetch next from promoter_cursor into @temp --转到下一个游标
        end
      --业务--
      insert into SmartPromoter([Name],[Mobile],[Address],[Status],[Account],[Password],[CreateTime],[Wechat],[CustomerID],[Balance])
        select a.Name,a.Mobile,a.Address,1,a.Mobile,'123456',a.CreateTime,a.WeChat,a.ID,0 from SmartCustomer a where  not exists (select ID from SmartPromoter b where a.ID=b.CustomerID) and LEN(Mobile)=11
       if @error=0
          begin
            commit tran   --提交事务
          end
          else
          begin
            rollback tran --回滚事务
          end
      close promoter_cursor  --关闭游标
      deallocate promoter_cursor   --释放游标
    end
  • 相关阅读:
    再谈PHP、Python与Ruby
    php消息队列
    创业如同追女生:成功的创业者都是追女生好手
    Windows 下 Python easy_install 的安装
    常用Python第三方库 简介
    Highcharts使用手册
    sqoop java api
    redis 读写分离
    linux 安装redis
    hive 安装和部署
  • 原文地址:https://www.cnblogs.com/xiaopotian/p/6825507.html
Copyright © 2011-2022 走看看