Я пытаюсь получить доступ к столбцу таблицы tb_client_cdc внутри функции (= хранимая процедура), которая будет вызываться триггером для сохранения последнего состояния примененной операции (UPDATE, INSERT, DELETE)в таблицу tb_client.
Если мы сделаем INSERT в tb_client, а строка существует в tb_client_cdc, нам не нужно вставлять другую строку, вместо этого мы должны ОБНОВИТЬ эту строку.
Итак, чтобы определить, существует ли строка, я использую:
IF ((TG_OP = 'INSERT') AND (sale_cdc.tb_client_cdc.saleclient_code <> NEW.client_code)) THEN
Я попытался поместить "sale_cdc.tb_client_cdc.saleclient_code" вместо sale_cdc.tb_client_cdc.saleclient_code, но это не решило мою проблему.
CREATE TABLE sale.tb_client
(
client_code CHAR(5) NOT NULL ,
client_name CHARACTER VARYING(40) NOT NULL ,
address CHARACTER VARYING(140) ,
city CHARACTER VARYING(25) ,
country CHARACTER VARYING(60) NOT NULL ,
contact_email CHARACTER VARYING(100) ,
phone CHARACTER VARYING(15) ,
parent_client_code CHAR(5) ,
created_by_user CHARACTER VARYING(10) NOT NULL DEFAULT 'OS_SYSTEM',
created_date DATE ,
updated_date DATE ,
CONSTRAINT pk_client PRIMARY KEY (client_code) ,
CONSTRAINT fk_client_parent FOREIGN KEY (parent_client_code)
REFERENCES sale.tb_client (client_code)
);
CREATE TABLE sale_cdc.tb_client_cdc
(
client_code CHAR(5) NOT NULL ,
client_name CHARACTER VARYING(40) NOT NULL ,
address CHARACTER VARYING(120) ,
city CHARACTER VARYING(25) ,
country CHARACTER VARYING(50) NOT NULL ,
contact_email CHARACTER VARYING(100) ,
phone CHARACTER VARYING(15) ,
parent_client_code CHAR(5) ,
created_by_user CHARACTER VARYING(10) NOT NULL DEFAULT 'OS_SYSTEM',
created_date DATE ,
updated_date DATE ,
operation CHAR(1) NOT NULL ,
user_id CHARACTER VARYING(10) NOT NULL ,
operation_timestamp TIMESTAMP(6) WITHOUT TIME ZONE NOT NULL , --timestamp [ (p) ] [ without time zone ]
CONSTRAINT pk_client PRIMARY KEY (client_code) ,
CONSTRAINT fk_client_parent FOREIGN KEY (parent_client_code)
REFERENCES sale_cdc.tb_client_cdc (client_code)
);
CREATE OR REPLACE FUNCTION update_tb_client_cdc() RETURNS TRIGGER AS
$$
BEGIN
IF ((TG_OP = 'INSERT') AND (sale_cdc.tb_client_cdc.saleclient_code <> NEW.client_code)) THEN
INSERT INTO sale_cdc.tb_client_cdc VALUES(NEW.*, 'I', user, now());
RETURN NEW;
END IF;
ELSIF ((TG_OP = 'INSERT') AND (sale_cdc.tb_client_cdc.client_code = NEW.client_code)) THEN
UPDATE sale_cdc.tb_client_cdc
SET client_name = NEW.client_name,
address = NEW.address,
city = NEW.city,
country = NEW.country,
contact_email = NEW.contact_email,
phone = NEW.phone,
parent_client_code = NEW.parent_client_code,
created_by_user = NEW.created_by_user,
created_date = NEW.created_date,
updated_date = NEW.updated_date,
operation = 'I',
user_id = user,
operation_timestamp = now()
WHERE client_code = NEW.client_code;
IF NOT FOUND THEN RETURN NULL;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER tr_client
AFTER INSERT OR UPDATE OR DELETE ON sale.tb_client
FOR EACH ROW EXECUTE PROCEDURE update_tb_client_cdc();
Ошибка:
ERROR: missing an entry for the table «tb_client_cdc» in the clause FROM
LINE 1: SELECT (((TG_OP = 'INSERT') AND (sale_cdc.tb_client_cdc.clie...
^
Или:
ERROR: there is no column «sale_cdc.tb_client_cdc.client_code»
LINE 1: SELECT (((TG_OP = 'INSERT') AND ("sale_cdc.tb_client_cdc.cli...
^