--
-- Name: age_group_changealias(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE or replace FUNCTION age_group_changealias() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
Declare	sqltext varchar; 
BEGIN

	sqltext := (select 'ALTER TABLE FIES.T_' || e.EntName || ' RENAME COLUMN ' || Old.Alias || ' TO ' || New.Alias || ';'
				from VCLib.T_ENTITIES e
				where e.EntName in ( 'MO_PEOPLE' ));
	EXECUTE sqltext;


	Return New;

END;
$$;



--
-- Name: age_group_changealias; Type: TRIGGER; Schema: public; Owner: sa
--
DO 
$$ 
BEGIN 
IF EXISTS(SELECT * 
                     FROM information_schema.triggers 
                     WHERE event_object_table = lower('T_AGE_GROUP') 
                     AND trigger_name = lower('age_group_changealias')
                     ) 
                    THEN 
                    DROP TRIGGER age_group_changealias ON FIES.T_AGE_GROUP;
    END IF ; 

END; 
$$ ;

CREATE TRIGGER age_group_changealias AFTER UPDATE OF alias ON FIES.T_AGE_GROUP FOR EACH ROW EXECUTE PROCEDURE age_group_changealias();
