DROP FUNCTION IF EXISTS akuz.RESOURCE_QUOTA_GROUP_Check CASCADE;

CREATE FUNCTION akuz.RESOURCE_QUOTA_GROUP_Check()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
	if exists
	(
		select* from AKUZ.T_RESOURCE_QUOTA 
		where q.Channel <> 5 
		and q.ResourceQuotaID = New.ResourceQuota
	)
	then
		Raise Exception Using Message = 'Квоту на группу можно установить только для канала Доктор.';
	end if;
	Return Old;
END;
$BODY$;
--ALTER FUNCTION akuz.RESOURCE_QUOTA_GROUP_Check() OWNER TO postgres;

-- DROP TRIGGER RESOURCE_QUOTA_GROUP_aftercheck ON AKUZ.T_RESOURCE_QUOTA_GROUP;
CREATE TRIGGER RESOURCE_QUOTA_GROUP_aftercheck
    AFTER INSERT OR UPDATE
    ON AKUZ.T_RESOURCE_QUOTA_GROUP
    FOR EACH ROW
    EXECUTE PROCEDURE akuz.RESOURCE_QUOTA_GROUP_Check();