存储过程 游标嵌套循环

ALTER PROCEDURE [dbo].[XXXXXX]
    
    
AS
    declare @startNum                nvarchar (255)
    declare @endNum                 nvarchar (255)
    declare @insurancetype            nvarchar (255)
    declare @company_ID                nvarchar(255)
    declare @unit_ID                nvarchar (255)
    declare @insuranceDivide_ID int
    declare @certificateNoClass        nvarchar(255)
    declare @st_nm int
    declare @end_nm int
    declare @cur_num                nvarchar(10)
    declare @currenDate                nvarchar(100) --保存当前时间
    declare @num_len int --变量长度
    declare @curren_len int --当前长度
    declare @i                        int --测试临时变量
    declare @temp                    nvarchar(8) --测试临时变量
    declare @temp_char                nvarchar(8) --测试临时变量
    
    --declare insur_cursor cursor for select startNum,endNum,insurancetype,Company_ID,unitID,certificateNoClass FROM InsuranceDivide where convert(int,endnum)-convert(int,startnum) -[usedAmount]>0
    declare insur_cursor cursor for select startNum,endNum,insurancetype,Company_ID,unitID,certificateNoClass,insuranceDivide_ID FROM InsuranceDivide where Company_ID='dtbx' and usedAmount<balance and insurancetype in ('142','209','239','240','241','242')
    
BEGIN
    
Open insur_cursor
fetch next from insur_cursor into @startNum,@endNum,@insurancetype,@company_ID,@unit_ID,@certificateNoClass,@insuranceDivide_ID
while @@fetch_status = 0
    begin
    
         set @currenDate=CONVERT(varchar(100), GETDATE(), 23)
            
            print @currenDate
            set @st_nm=convert(int,@startNum)
            set @end_nm=convert(int,@endNum)
            set @temp=0
            set @i=1;
            while @st_nm<=@end_nm
             begin                    
                    set @num_len=len(@endNum);
                    set @curren_len=len(@st_nm)                    
                    while @curren_len<(@num_len)
                     begin
                            set @temp_char=convert(nvarchar(8),@st_nm)
                            set @temp_char='0'+@temp_char                            
                            set @curren_len=@curren_len+1
                     end        
                    
                     declare t_cur cursor for select count(*) as total from insurance where ticketid=@temp_char and insurancetype=@insurancetype and company_id=Company_ID
                    
                    
                         open t_cur
                     fetch next from t_cur    into @cur_num    
                     if @cur_num=0
                     begin
                                 print @temp_char+' @cur_num '+@cur_num
                                 insert into invoiceNoManager(invoiceNo,certificateNoClass,InsuranceDivide_ID,SuspendType,unit_ID,company_ID,UsedStatus,CreateTime,LinesStatus,InsuranceType)
                        values(@st_nm,@certificateNoClass,@insuranceDivide_ID,2,@unit_ID,@company_ID,1,'2012-09-26',0,@insurancetype)
                                 fetch next from t_cur    into @cur_num
                     end
                     close t_cur
                     deallocate t_cur
                    
                            set @st_nm= @st_nm+1
             end
            
         fetch next from insur_cursor into @startNum,@endNum,@insurancetype,@company_ID,@unit_ID,@certificateNoClass,@insuranceDivide_ID
        
    end
    close insur_cursor
     deallocate insur_cursor
    
END
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值