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
  • 相关阅读:
    要学习编程?这10件事情你知道了吗?
    JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
    这8个免费的网上课程可以有助你的技术成长
    给游戏开发初学者的10条建议
    21个国外受欢迎的学习编程的网站:总有一个理由会让你爱上它们
    hibernate 知识梳理
    struts2 知识梳理
    maven 相关
    c#配置log4net步骤
    arcobject 相关
  • 原文地址:https://www.cnblogs.com/xiaopotian/p/6825507.html
Copyright © 2011-2022 走看看