存储过程 游标嵌套循环

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
MySQL存储过程可以使用游标来进行数据的遍历和操作。游标嵌套是指在一个游标循环内部再使用一个或多个游标循环,以实现更复杂的数据操作。 下面是一个简单的示例,演示了如何在MySQL存储过程中嵌套使用游标: ``` DELIMITER // CREATE PROCEDURE nested_cursors() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE outer_cursor CURSOR FOR SELECT id FROM table1; DECLARE inner_cursor CURSOR FOR SELECT value FROM table2 WHERE id = @current_id; OPEN outer_cursor; outer_loop: LOOP FETCH outer_cursor INTO @current_id; IF done THEN LEAVE outer_loop; END IF; OPEN inner_cursor; inner_loop: LOOP FETCH inner_cursor INTO @current_value; IF done THEN LEAVE inner_loop; END IF; -- 在这里进行数据操作,例如将数据插入到一个临时表中 END LOOP; CLOSE inner_cursor; END LOOP; CLOSE outer_cursor; END// DELIMITER ; ``` 在这个示例中,我们定义了两个游标:`outer_cursor` 和 `inner_cursor`。`outer_cursor` 用于遍历 `table1` 表中的所有记录,而 `inner_cursor` 则用于遍历 `table2` 表中与当前 `outer_cursor` 记录相关联的所有记录。 在存储过程的主循环中,我们首先打开 `outer_cursor`,然后进入一个无限循环,直到所有记录都被遍历完毕。在每次循环中,我们使用 `FETCH` 命令获取当前记录的 `id` 值,并将其存储在变量 `@current_id` 中。 接下来,我们打开 `inner_cursor`,并进入另一个无限循环,直到所有与当前 `@current_id` 相关联的记录都被遍历完毕。在每次循环中,我们使用 `FETCH` 命令获取当前记录的 `value` 值,并将其存储在变量 `@current_value` 中。 最后,在内部循环中,我们可以使用 `@current_id` 和 `@current_value` 变量进行数据操作,例如将数据插入到一个临时表中。完成内部循环后,我们关闭 `inner_cursor`,回到外部循环中,继续遍历下一个记录,直到所有记录都被遍历完毕。 注意,为了避免游标嵌套时出现问题,我们需要在每个游标的内部循环中使用唯一的变量名,否则可能会出现变量名冲突的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值