DROP PROCEDURE IF EXISTS public.sp_deletefrllo;
DROP PROCEDURE IF EXISTS dbo.sp_deletefrllo;
-- =============================================
-- Author:      Vitacore (с) Anatoliy
-- Create date: 2021 02 17
-- Description: Удаление данных ФРЛЛО
-- =============================================
CREATE OR REPLACE PROCEDURE rds.sp_deletefrllo(
    "Id" uuid DEFAULT NULL::uuid
)
    LANGUAGE plpgsql
AS
$$
BEGIN
    LOOP
        EXIT WHEN NOT EXISTS(SELECT 1 FROM rds.t_export_frllo_data WHERE exportfrllo = "Id" LIMIT 1)
        ;WITH to_delete AS (SELECT exportfrllodataid AS id
                           FROM rds.t_export_frllo_data
                           WHERE exportfrllo = "Id"
                           LIMIT 10000
                          )
        DELETE
        FROM rds.t_export_frllo_data d
            USING to_delete
        WHERE d.exportfrllodataid = to_delete.id;
    END LOOP;
    
    LOOP
        EXIT WHEN NOT EXISTS(SELECT 1 FROM rds.t_export_frllo WHERE exportfrlloid = "Id" LIMIT 1)
        
        ;WITH to_delete AS (SELECT f.exportfrlloid
                           FROM rds.t_export_frllo f
                           WHERE f.exportfrlloid = "Id"
                           LIMIT 10000
                          )
        DELETE
        FROM rds.t_export_frllo f
            USING to_delete
        WHERE f.exportfrlloid = to_delete.exportfrlloid;
    
    
    END LOOP;
END;
$$;