一般错误:3636 递归查询在 1001 次迭代后中止

问题描述 投票:0回答:1

执行该过程导致此错误:

Unhandled Exception.
SQLSTATE[HY000]: General error: 3636 Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value.
You can find the error back in the log.
CREATE DEFINER=`root`@`localhost` PROCEDURE `PR_Listado_Validar`(
    IN `v_user_id` INT    
)

BEGIN
    declare v_perfil_adm_id int;    
    declare v_perfil_dir_id int;
    declare v_perfil_rrhh_id int;
    declare v_EsAdministrador bit;
    declare v_EsDirector bit;
    declare v_EsRRHH bit;
    
    select perfil_id into v_perfil_adm_id
    from ch_perfiles
    where ds_perfil like '%Administrador%';
    
    if exists (select * from ch_users_perfiles
                where user_id = v_user_id
                and perfil_id = v_perfil_adm_id
              )
    then 
        set v_EsAdministrador = 1;
    else 
        set v_EsAdministrador = 0;
    end if;
    
    select perfil_id into v_perfil_dir_id
    from ch_perfiles
    where ds_perfil like '%Director%';
    
    if exists (select * from ch_users_perfiles
                where user_id = v_user_id
                and perfil_id = v_perfil_dir_id
              )
    then 
        set v_EsDirector = 1;
    else 
        set v_EsDirector = 0;
    end if;
    
    select perfil_id into v_perfil_rrhh_id
    from ch_perfiles
    where ds_perfil like '%RRHH%';
    
    if exists (select * from ch_users_perfiles
                where user_id = v_user_id
                and perfil_id = v_perfil_rrhh_id
              )
    then 
        set v_EsRRHH = 1;
    else 
        set v_EsRRHH = 0;
    end if;
    
    create temporary table Temp_Result 
        (
          user_id int(11) NOT NULL 
          , num_trabajador_superior varchar(255) NULL
          , apellidos_nombre varchar(255) NOT NULL
          , Tipo varchar(7) NOT NULL 
          , hora_id int(11) NOT NULL 
          , hora_pausa_id int(11) NULL
          , fecha_inicio datetime NOT NULL
          , fecha_fin datetime NULL
          , pausa_id int(11) NULL
          , tipo_de_pausa varchar(255) null
          , hora_inicio varchar(10) NULL
          , hora_inicio_ant varchar(10) NULL
          , hora_fin varchar(10) NULL
          , hora_fin_ant varchar(10) NULL
          , tiempo varchar(60) NULL
          , Editable bit(1) NOT NULL
          , Activo bit(1) NOT NULL
          , Validado bit(1) NOT NULL
          , ParaEliminar bit(1) NOT NULL
          , hora_pausa_id_ant int(11) NULL
        );  
    if v_EsAdministrador = 1
    then        
    insert into Temp_Result
        (
          user_id
          , num_trabajador_superior
          , apellidos_nombre
          , Tipo
          , hora_id
          , hora_pausa_id         
          , fecha_inicio
          , fecha_fin   
          , pausa_id
          , tipo_de_pausa
          , hora_inicio
          , hora_inicio_ant
          , hora_fin
          , hora_fin_ant
          , tiempo
          , Editable
          , Activo
          , Validado
          , ParaEliminar
          , hora_pausa_id_ant 
        )
        select todo.user_id
            , u.num_trabajador_superior
            , u.apellidos_nombre
            , Tipo
            , hora_id
            , hora_pausa_id
            , fecha_inicio
            , fecha_fin
            , pausa_id
            , tipo_de_pausa
            , hora_inicio
            , hora_inicio_ant
            , hora_fin
            , hora_fin_ant
            , tiempo
            , Editable
            , Activo
            , Validado
            , ParaEliminar
            , hora_pausa_id_ant
        from
        (
        select user_id
            , 'Pausa' as Tipo
            , h.hora_id
            , p.hora_pausa_id
            , p.fecha_inicio
            , p.fecha_fin
            , pt.pausa_id as pausa_id
            , pt.ds_pausa as tipo_de_pausa
            , DATE_FORMAT(p.fecha_inicio, "%H:%i") as hora_inicio
            , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_inicio_ant
            , DATE_FORMAT(p.fecha_fin, "%H:%i") as hora_fin
            , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_fin_ant
            , CONCAT(
                    IF(
                        MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24) = 0
                        , ''
                        , CONCAT(MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24), ' horas y ')
                      )
                    ,
                    MOD( TIMESTAMPDIFF(minute,p.fecha_inicio,p.fecha_fin), 60), ' minutos '
                    ) as tiempo
            , p.Editable
            , p.Activo
            , p.Validado
            , p.ParaEliminar
            , p.hora_pausa_id_ant
            from ch_users_registro_horas_testvalidar as h
            inner join ch_users_registro_horas_pausas_testvalidar as p
            on h.hora_id = p.hora_id
            inner join ch_pausas_tipos as pt
            on p.pausa_id = pt.pausa_id
            where p.Validado = 0 AND h.fecha_inicio > '2024-01-01 00:00:00'         
            union all           
            select 
              user_id
            , 'Jornada' as Tipo
            , hora_id
            , null as hora_pausa_id
            , fecha_inicio
            , fecha_fin     
            , null as pausa_id
            , null as tipo_de_pausa
            , DATE_FORMAT(fecha_inicio, "%H:%i") as hora_inicio
            , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_inicio_ant
            , DATE_FORMAT(fecha_fin, "%H:%i") as hora_fin
            , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_fin_ant
            , CONCAT(
                    IF(
                        MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24) = 0
                        , ''
                        , CONCAT(MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24), ' horas y ')
                      )
                    ,
                    MOD( TIMESTAMPDIFF(minute,fecha_inicio,fecha_fin), 60), ' minutos '
                    ) as tiempo
            , h.Editable
            , h.Activo
            , h.Validado
            , h.ParaEliminar
            , h.hora_id_ant
            from ch_users_registro_horas_testvalidar as h
            where h.Validado = 0 AND h.fecha_inicio > '2024-01-01 00:00:00' 
        ) as todo
        inner join
        v_jerarquia_usuarios as j
        on todo.user_id = j.user_id
        inner join
        ch_users as u
        on todo.user_id = u.user_id;
    end if;
    if v_EsDirector = 1 or v_EsRRHH = 1
    then          
        create table Temp_Empresas_Director
        (empresa_id int);         
        insert into Temp_Empresas_Director
        (empresa_id)
        select empresa_id 
            from ch_users_empresas_unidades
            where user_id = v_user_id;
        insert into Temp_Result
            (
              user_id
              , num_trabajador_superior
              , apellidos_nombre
              , Tipo
              , hora_id
              , hora_pausa_id
              , fecha_inicio
              , fecha_fin
              , pausa_id
              , tipo_de_pausa
              , hora_inicio
              , hora_inicio_ant
              , hora_fin
              , hora_fin_ant
              , tiempo
              , Editable
              , Activo
              , Validado
              , ParaEliminar
              , hora_pausa_id_ant 
            )
            select todo.user_id
                , u.num_trabajador_superior
                , u.apellidos_nombre
                , Tipo
                , hora_id
                , hora_pausa_id
                , fecha_inicio
                , fecha_fin
                , pausa_id
                , tipo_de_pausa
                , hora_inicio
                , hora_inicio_ant
                , hora_fin
                , hora_fin_ant
                , tiempo
                , Editable
                , Activo
                , Validado
                , ParaEliminar
                , hora_pausa_id_ant
                from
            (
            select user_id
                , 'Pausa' as Tipo
                , h.hora_id
                , p.hora_pausa_id
                , p.fecha_inicio
                , p.fecha_fin
                , pt.pausa_id as pausa_id
                , pt.ds_pausa as tipo_de_pausa
                , DATE_FORMAT(p.fecha_inicio, "%H:%i") as hora_inicio
               , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_inicio_ant
                , DATE_FORMAT(p.fecha_fin, "%H:%i") as hora_fin
               , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_fin_ant
                , CONCAT(
                        IF(
                            MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24) = 0
                            , ''
                            , CONCAT(MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24), ' horas y ')
                          )
                        ,
                        MOD( TIMESTAMPDIFF(minute,p.fecha_inicio,p.fecha_fin), 60), ' minutos '
                        ) as tiempo
                , p.Editable
                , p.Activo
                , p.Validado
                , p.ParaEliminar
                , p.hora_pausa_id_ant
                from ch_users_registro_horas_testvalidar as h
                inner join ch_users_registro_horas_pausas_testvalidar as p
                on h.hora_id = p.hora_id
                inner join ch_pausas_tipos as pt
                on p.pausa_id = pt.pausa_id
                where p.Validado = 0 AND h.fecha_inicio > '2024-01-01 00:00:00'     
            union all           
            select 
                  user_id
                , 'Jornada' as Tipo
                , hora_id
                , null as hora_pausa_id
                , fecha_inicio
                , fecha_fin     
                , null as pausa_id
                , null as tipo_de_pausa
                , DATE_FORMAT(fecha_inicio, "%H:%i") as hora_inicio
                , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_inicio_ant
                , DATE_FORMAT(fecha_fin, "%H:%i") as hora_fin
                , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_fin_ant
                , CONCAT(
                        IF(
                            MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24) = 0
                            , ''
                            , CONCAT(MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24), ' horas y ')
                          )
                        ,
                        MOD( TIMESTAMPDIFF(minute,fecha_inicio,fecha_fin), 60), ' minutos '
                        ) as tiempo
                , h.Editable
                , h.Activo
                , h.Validado
                , h.ParaEliminar
                , h.hora_id_ant
            from ch_users_registro_horas_testvalidar as h
            where h.Validado = 0 AND h.fecha_inicio > '2024-01-01 00:00:00' 
            ) as todo
            inner join
            ch_users_empresas_unidades as eu
            on todo.user_id = eu.user_id
            inner join Temp_Empresas_Director as ed                
            on eu.empresa_id = ed.empresa_id            
            inner join
            ch_users as u
            on todo.user_id = u.user_id;            
            drop table Temp_Empresas_Director;            
         end if;
        if v_EsDirector = 0 
        and v_EsAdministrador = 0
        then
        insert into Temp_Result
        (
          user_id
          , num_trabajador_superior
          , apellidos_nombre
          , Tipo
          , hora_id
          , hora_pausa_id
          , fecha_inicio
          , fecha_fin
          , pausa_id
          , tipo_de_pausa
          , hora_inicio
          , hora_inicio_ant
          , hora_fin
          , hora_fin_ant
          , tiempo
          , Editable
          , Activo
          , Validado
          , ParaEliminar
          , hora_pausa_id_ant 
        )
        select todo.user_id
            , u.num_trabajador_superior
            , apellidos_nombre
            , Tipo
            , hora_id
            , hora_pausa_id
            , fecha_inicio
            , fecha_fin
            , pausa_id
            , tipo_de_pausa
            , hora_inicio
            , hora_inicio_ant
            , hora_fin
            , hora_fin_ant
            , tiempo
            , Editable
            , Activo
            , Validado
            , ParaEliminar
            , hora_pausa_id_ant
        from
        (
        select user_id
            , 'Pausa' as Tipo
            , h.hora_id
            , p.hora_pausa_id
            , p.fecha_inicio
            , p.fecha_fin
            , pt.pausa_id as pausa_id
            , pt.ds_pausa as tipo_de_pausa
            , DATE_FORMAT(p.fecha_inicio, "%H:%i") as hora_inicio
            , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_inicio_ant
            , DATE_FORMAT(p.fecha_fin, "%H:%i") as hora_fin
            , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_pausas_testvalidar as r where r.hora_pausa_id = p.hora_pausa_id_ant) as hora_fin_ant
            , CONCAT(
                    IF(
                        MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24) = 0
                        , ''
                        , CONCAT(MOD( TIMESTAMPDIFF(hour,p.fecha_inicio,p.fecha_fin), 24), ' horas y ')
                      )
                    ,
                    MOD( TIMESTAMPDIFF(minute,p.fecha_inicio,p.fecha_fin), 60), ' minutos '
                    ) as tiempo
            , p.Editable
            , p.Activo
            , p.Validado
            , p.ParaEliminar
            , p.hora_pausa_id_ant
            from ch_users_registro_horas_testvalidar as h
            inner join ch_users_registro_horas_pausas_testvalidar as p
            on h.hora_id = p.hora_id
            inner join ch_pausas_tipos as pt
            on p.pausa_id = pt.pausa_id
            where p.Validado = 0 AND h.fecha_inicio > '2024-01-01 00:00:00'                     
            union all           
            select 
              user_id
            , 'Jornada' as Tipo
            , hora_id
            , null as hora_pausa_id
            , fecha_inicio
            , fecha_fin     
            , null as pausa_id
            , null as tipo_de_pausa
            , DATE_FORMAT(fecha_inicio, "%H:%i") as hora_inicio
            , (Select DATE_FORMAT(fecha_inicio, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_inicio_ant
            , DATE_FORMAT(fecha_fin, "%H:%i") as hora_fin
            , (Select DATE_FORMAT(fecha_fin, "%H:%i") from ch_users_registro_horas_testvalidar as r where r.hora_id = h.hora_id_ant) as hora_fin_ant
            , CONCAT(
                    IF(
                        MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24) = 0
                        , ''
                        , CONCAT(MOD( TIMESTAMPDIFF(hour,fecha_inicio,fecha_fin), 24), ' horas y ')
                      )
                    ,
                    MOD( TIMESTAMPDIFF(minute,fecha_inicio,fecha_fin), 60), ' minutos '
                    ) as tiempo
            , h.Editable
            , h.Activo
            , h.Validado
            , h.ParaEliminar
            , h.hora_id_ant
            from ch_users_registro_horas_testvalidar as h
            where h.Validado = 0  AND h.fecha_inicio > '2024-01-01 00:00:00'    
        ) as todo
        inner join
        v_jerarquia_usuarios as j
        on todo.user_id = j.user_id     
        inner join
        ch_users as u
        on todo.user_id = u.user_id
        where 
        j.user_id_superior = v_user_id;        
    end if;
    
    select distinct 
          user_id
          , num_trabajador_superior
          , apellidos_nombre
          , Tipo
          , hora_id
          , hora_pausa_id
          , fecha_inicio
          , fecha_fin
          , pausa_id
          , tipo_de_pausa
          , hora_inicio
          , hora_inicio_ant
          , hora_fin
          , hora_fin_ant
          , tiempo
          , Editable
          , Activo
          , Validado
          , ParaEliminar
          , hora_pausa_id_ant 
          from Temp_Result
          WHERE fecha_inicio > '2024-01-01 00:00:00'
        order by hora_id, Tipo, fecha_inicio;
        
    drop table Temp_Result;
END

我尝试将

@@cte_max_recursion_depth
增加到更大的值但不起作用,我还尝试减少数据库的行数。

PS:剧本不是我的,他们告诉我这是工作很久之前的

sql mysql recursive-query
1个回答
0
投票

更新:该过程正在使用视图(在内部联接中),该视图是问题的原因,但我仍然不知道如何修复它。

景色:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_jerarquia_usuarios` AS with recursive `cte` as (select `v_ch_users`.`user_id` AS `user_id`,`v_ch_users`.`num_trabajador` AS `num_trabajador`,`v_ch_users`.`user_id_superior` AS `user_id_superior`,`v_ch_users`.`num_trabajador_superior` AS `num_trabajador_superior`,1 AS `nivel` from `v_ch_users` where (`v_ch_users`.`num_trabajador_superior` is not null) union all select `c`.`user_id` AS `user_id`,`c`.`num_trabajador` AS `num_trabajador`,`u`.`user_id_superior` AS `user_id_superior`,`u`.`num_trabajador_superior` AS `num_trabajador_superior`,(`c`.`nivel` + 1) AS `nivel` from (`cte` `c` join `v_ch_users` `u` on((`c`.`num_trabajador_superior` = `u`.`num_trabajador`)))) select `cte`.`user_id` AS `user_id`,`cte`.`num_trabajador` AS `num_trabajador`,`cte`.`user_id_superior` AS `user_id_superior`,`cte`.`num_trabajador_superior` AS `num_trabajador_superior`,`cte`.`nivel` AS `nivel` from `cte`
© www.soinside.com 2019 - 2024. All rights reserved.