How to find owning schema in function
How to find owning schema in function
От:
"Andrus" <kobruleht2@hot.ee>
Дата:
8.1+ database contains separate schemas for every company named company1, company2, .... companyi. order tables in those schemas contain trigger like for company1: CREATE OR REPLACE FUNCTION dok_seq_trig() RETURNS "trigger" AS $$BEGIN IF NEW.tasudok IS NULL AND NEW.doktyyp!='O' THEN NEW.tasudok = nextval( 'company1.'|| TG_RELNAME || '_'|| NEW.doktyyp ||'_seq'); END IF; IF NEW.arvenumber IS NULL AND NEW.doktyyp='O' THEN NEW.arvenumber = nextval( 'company1.'|| TG_RELNAME || '_'|| NEW.doktyyp ||'_seq'); END IF; RETURN NEW; END$$ LANGUAGE plpgsql STRICT; This code has hard coded schema name 'company1' . If new company schema n is created from existing one, trigger functions needs manual update to change schema to companyn. How to change this code so that instead of hard-coded schema name it automatically uses the schema where trigger function is defined ?
Re: How to find owning schema in function
От:
Sim Zacks <sim@compulab.co.il>
Дата:
On 11/05/2011 05:36 PM, Andrus wrote:8.1+ database contains separate schemas for every company named company1, company2, .... companyi.http://www.postgresql.org/docs/8.2/interactive/plpgsql-trigger.html
order tables in those schemas contain trigger like for company1:
CREATE OR REPLACE FUNCTION dok_seq_trig() RETURNS "trigger"
AS $$BEGIN
IF NEW.tasudok IS NULL AND NEW.doktyyp!='O' THEN
NEW.tasudok = nextval( 'company1.'|| TG_RELNAME || '_'|| NEW.doktyyp ||'_seq');
END IF;
IF NEW.arvenumber IS NULL AND NEW.doktyyp='O' THEN
NEW.arvenumber = nextval( 'company1.'|| TG_RELNAME || '_'|| NEW.doktyyp ||'_seq');
END IF;
RETURN NEW;
END$$ LANGUAGE plpgsql STRICT;
This code has hard coded schema name 'company1' . If new company schema n is created from existing one, trigger functions needs manual update to change schema to companyn.
How to change this code so that instead of hard-coded schema name it automatically uses the schema where trigger function is defined ?
Shows all available variables. In this case - TG_TABLE_SCHEMA
Sim