这个触发器无法插入触发器表有什么问题?

时间:2016-03-04 06:28:49

标签: postgresql triggers

CREATE OR REPLACE FUNCTION insert_data_into_stock_ledger_for_opstock()
  RETURNS trigger AS
$BODY$
 DECLARE
    stocid integer;
    rec1 record;
 BEGIN
    FOR rec1 IN
    select * from mas_stock
    where stock_id = NEW.stock_id
    LOOP
        INSERT into stock_leger_head (tra_id,tra_type,date,dept_id,flag,item_id,qty) values (NEW.stock_id,'OS',NEW.created_datetime,NEW.dept_id,'I',rec1.item_id,rec1.qty) RETURNING stock_id INTO stocid;
        /*IF rec1.model_no IS NULL OR rec1.model_no = ''
        THEN
            RETURN NEW;
        ELSE*/
            INSERT into stock_leger_detail (stock_id,model_no,serial_batch,qty) values (stocid,rec1.model_no,rec1.serial_batch_no,rec1.qty);
        /*END IF; */
    END LOOP;
    RETURN NEW;
 END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION insert_data_into_stock_ledger_for_opstock()
  OWNER TO postgres;

这是我第一次使用触发器我不知道出了什么问题。甚至在插入时也没有显示任何错误。数据插入mas_stock工作正常但不触发??

2 个答案:

答案 0 :(得分:1)

必须添加

 CREATE TRIGGER (TRIGGER_NAME) before delete on (NAME_TABLE)
FOR EACH ROW EXECUTE PROCEDURE insert_data_into_stock_ledger_for_opstock();

答案 1 :(得分:1)

CREATE TRIGGER insert_opening_stock     插入mas_stock后     对于每一行     执行程序insert_data_into_stock_ledger_for_opstock();