日期:2014-05-18  浏览次数:20486 次

sql语句循环嵌套
SQL code

declare @next int
    declare @CurrentId int
    set @next=1
    while @next<=dbo.Get_StrArrayLength(@ReterId,',')
    begin
        set @CurrentId=dbo.Get_StrArrayStrOfIndex(@ReterId,',',@next)
        INSERT INTO [L_ReterInfo](
        [CreateTime],[UserProId],[ReterId],[Weight],[Status],[Remark]
        )VALUES(
        GETDATE(),@Id,@CurrentId,@ReterWeight,@Status,@Remark
        )
        
        declare @nexts int    
        declare @CurrentIdss int
        while @nexts<=dbo.Get_StrArrayLength(@ByReterId,',')
        begin
            set @CurrentIdss=dbo.Get_StrArrayStrOfIndex(@ByReterId,',',@next)
            INSERT INTO [L_ByReterInfo](
            [CreateTime],[UserProId],[ReterId],[ByReterId]
            )VALUES(
            GETDATE(),@Id,@CurrentId,@CurrentIdss
            )
            set @nexts=@nexts+1
        end
        set @next=@next+1
    end



循环里面的内层循环为什么不执行?

------解决方案--------------------
SQL code


declare @next int
declare @CurrentId int
declare @nexts int    
declare @CurrentIdss int
declare @End1    int
declare @End2    int
set @next=1
set @End1 = dbo.Get_StrArrayLength(@ReterId,',')

while @next<=@End1
begin
    set @CurrentId=dbo.Get_StrArrayStrOfIndex(@ReterId,',',@next)
    INSERT INTO [L_ReterInfo](
    [CreateTime],[UserProId],[ReterId],[Weight],[Status],[Remark]
    )VALUES(
    GETDATE(),@Id,@CurrentId,@ReterWeight,@Status,@Remark
    )
    set @nexts = 1
    set @End2 = =dbo.Get_StrArrayLength(@ByReterId /*这在那里定义 赋值的?*/,',')
    while @nexts<=@End2
    begin
        set @CurrentIdss=dbo.Get_StrArrayStrOfIndex(@ByReterId,',',@next)
        INSERT INTO [L_ByReterInfo](
        [CreateTime],[UserProId],[ReterId],[ByReterId]
        )VALUES(
        GETDATE(),@Id,@CurrentId,@CurrentIdss
        )
        set @nexts=@nexts+1
    end
    set @next=@next+1
end