-- FUNCTION: fies.sp_setstatuses(uuid)

-- DROP FUNCTION fies.sp_setstatuses(uuid);

CREATE OR REPLACE FUNCTION fies.sp_DropSectionDependSettingsWithChilds(
	SectionDependSettingsID uuid)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100 
    VOLATILE 
AS $BODY$
DECLARE 
 sqltext varchar ;
BEGIN
sqltext := (select string_agg(CONCAT('(sds.', sectionAlias,'= dl.' , sectionAlias , ' OR sds.' , sectionAlias , ' IS NULL)') , ' AND ')
from FIES.T_PLAN_SECTION
);
sqltext:=('delete from fies.t_section_depend_settings where SectionDependSettingsID in (' ||
		  'select dl.SectionDependSettingsID 
		  from fies.t_section_depend_settings sds
		  join fies.t_section_depend_settings dl on ' || sqltext || ' where sds.SectionDependSettingsID = ''' || cast(SectionDependSettingsID as varchar) || ''')');
EXECUTE sqltext;
END;
$BODY$;

ALTER FUNCTION fies.sp_DropSectionDependSettingsWithChilds(uuid)
    OWNER TO postgres;
