2016-10-04 2 views
0

삽입 작업을위한 트리거와 삽입 된 값을 출력하는 절차를 만들고 싶습니다. 새 값을 트리거로 출력하는 방법 Postgres

CREATE TRIGGER added_product_info_trigger 
BEFORE INSERT 
ON products 
EXECUTE PROCEDURE added_product_info(); 

그리고 내 절차

CREATE OR REPLACE FUNCTION added_product_info() 
RETURNS trigger 
AS 
$$ 
    (Select p.productname, s.companyname from products as p, suppliers as s 
    where p.supplierid = s.supplierid) 
$$ LANGUAGE SQL; 

는 어떻게 삽입 값을 인쇄 할 수 있습니까?

+0

'인상 공지 ': https : // w ww.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTSRAISE –

답변

2

SQL 함수는 trigger을 반환 할 수 없습니다. 아마도 plpgsql 함수를 작성하려고했을 것입니다.

트리거 기능은 결과 (예 : select 쿼리의 결과)를 생성 할 수 없습니다. 당신은 클라이언트 프로그램에 어떤 결과를 전달하는 raise notice를 사용할 수 있습니다

create or replace function added_product_info() 
returns trigger as $$ 
declare 
    company text; 
begin 
    select companyname 
    from suppliers 
    where supplierid = new.supplierid 
    into company; 
    raise notice 'inserted: "%" supplied by "%"', new.productname, company; 
    return new; 
end; 
$$ language plpgsql; 

기록 new 레코드 newold에 액세스 할 수없는 경우 (기본값은 for each statement입니다 for each row 트리거가 선언 된 경우에만 트리거 기능에서 볼 수 있습니다) : 트리거가

create trigger added_product_info_trigger 
before insert on products 
for each row 
execute procedure added_product_info(); 

경우 before insert for each rownew을 반환해야합니다.

클라이언트는 통지를 받고 처리 할 준비가되어 있어야합니다. 당신은 표준 psql의 쉘 프로그램에서 쿼리를 실행하면 , 당신은 얻을 것이다 :

insert into products values ('some product', 1); 
NOTICE: inserted: "some product" supplied by "company" 
INSERT 0 1 

읽기 :

관련 문제