CREATE OR REPLACE PROCEDURE rds.sp_dndel(
    fileid uuid
)
    LANGUAGE plpgsql
AS
$$
DECLARE
    p_fileid uuid = fileid;
BEGIN
    DELETE
    FROM rds.t_dn_item_plan itemplan
    WHERE EXISTS (SELECT 1
                  FROM rds.t_dn_item dnitem
                  WHERE dnitem.dnitemid = itemplan.dnitem
                    AND dnitem.dnfile = p_fileid
                 );

    DELETE FROM rds.t_dn_item WHERE dnfile = p_fileid;

    DELETE FROM rds.t_dn_file f WHERE f.fileid = p_fileid;
END;
$$