2012-05-18 123 views
1

請幫我正確的SQL語句:PostgreSQL觸發通知

CREATE TRIGGER transaction_insert 
    AFTER INSERT ON transaction 
    FOR EACH ROW 
    WHEN (NEW.tr_type = 55) 
    EXECUTE PROCEDURE pg_notify('check_close', to_char(NEW.n_transaction, 999999)); 

我需要添加tr_type=55行時收到通知。 tr_typeint。 n_transaction是int

Postgresql 8.3.4

請幫忙!

+1

請注意, 「tramsaction」 是一個保留字。命名錶「交易」是一個不錯的選擇。 – wildplasser

回答

2

嘗試以下操作:

CREATE TRIGGER transaction_insert 
    AFTER INSERT ON transaction 
    FOR EACH ROW EXECUTE PROCEDURE transaction_insert_check_close(); 


CREATE OR REPLACE FUNCTION transaction_insert_check_close() RETURNS TRIGGER AS $transaction_insert_check_close$ 
BEGIN 
    IF (NEW.tr_type = 55) THEN 
     PERFORM pg_notify('check_close', to_char(NEW.n_transaction, 999999)); 
    END IF; 

    RETURN NEW; 
END; 

$transaction_insert_check_close$ LANGUAGE plpgsql;