Oracle触发器与null冲突

时间:2012-09-26 17:00:12

标签: sql oracle triggers null sqlplus

首先,这是一个作业,所以你想要建议的大多数非常酷的预先编写的函数,我将不被允许使用。

我有几张表都有字段

creation_date
created_by
last_update_date
last_updated_by

我需要编写一个触发器来填充这些内容以进行创建或更新。问题是,这些表的空值对我来说是个问题。例如:

CREATE TABLE parts
(
pno                NUMBER,
pname              VARCHAR2(50) NOT NULL,
qoh                NUMBER       NOT NULL,
price              NUMBER(5,2),
reorder_level      NUMBER(2),
creation_date      DATE         NOT NULL,
created_by         VARCHAR2(10) NOT NULL,
last_update_date   DATE         NOT NULL,
last_updated_by    VARCHAR2(10) NOT NULL,
CONSTRAINT parts_PK PRIMARY KEY (pno))

这些NOT NULL是给我的,我不允许改变它们。所以我很难将其概念化。

  • 如果我的触发器在创建字段之前添加了这些值,我就无法将这些字段设置为INSERT INTO,因为它们不是NULL。
  • 如果我的触发器在创建字段后添加了这些值,则会出现编译错误ORA-00903和00922.表名无效且选项无效。

我认为我的触发器看起来像

CREATE OR REPLACE TRIGGER pcreate
  BEFORE UPDATE on parts
  FOR EACH ROW
BEGIN
  UPDATE
    SET creation_date = SYSDATE;
    SET created_by = USER;
    SET last_update_date = SYSDATE;
    SET last_updated_by = USER;
END;
/

CREATE OR REPLACE TRIGGER pchange
  BEFORE UPDATE on parts
  FOR EACH ROW
BEGIN
  UPDATE
    SET last_update_date = SYSDATE;
    SET last_updated_by = USER;
END;
/
repeat for the other tables

我可能会被允许使用UPSERT,但我真的不知道它是如何工作的......任何建议都是受欢迎的。我真的在这里学习,所以任何其他建议都表示赞赏。

编辑:

我不会确认触发的包如下。我是否需要在包内调用触发器?

CREATE OR REPLACE PACKAGE process_orders 
AS
  PROCEDURE add_order (p_cno IN NUMBER, p_eno IN NUMBER, p_received IN DATE);
  PROCEDURE add_order_details (p_ono IN NUMBER, p_pno IN NUMBER, p_qty IN NUMBER);
  PROCEDURE ship_order (p_ono IN NUMBER, p_sdate IN DATE);
  PROCEDURE delete_order (p_ono IN NUMBER);
  FUNCTION total_emp_sales (f_eno IN NUMBER) RETURN NUMBER;
END process_orders;
/

CREATE OR REPLACE PACKAGE BODY process_orders 
AS
  PROCEDURE add_order (p_cno IN NUMBER, p_eno IN NUMBER, p_received IN DATE) 
  IS
  ao_emsg       VARCHAR2(100);
  p_rec_today   DATE;
  BEGIN
    SELECT SYSDATE INTO p_rec_today FROM dual;
    IF p_received is null THEN
        INSERT INTO orders (ono, cno, eno, received)
        VALUES(order_number_seq.NEXTVAL,p_cno,p_eno,p_rec_today);
    ELSE
        INSERT INTO orders (ono, cno, eno, received)
    VALUES(order_number_seq.NEXTVAL,p_cno,p_eno,p_received);
    END IF;

  EXCEPTION
    WHEN OTHERS THEN
      ao_emsg := substr(SQLERRM,1,100);
      INSERT INTO orders_errors (ono,transaction_date,message)
      VALUES(order_number_seq.CURRVAL,SYSDATE,ao_emsg);
  END;
  --Etc. Procedures
END;
/

2 个答案:

答案 0 :(得分:5)

在触发器中,您只想修改:new记录。你的触发器看起来像

CREATE OR REPLACE TRIGGER parts_before_insert
  BEFORE INSERT on parts
  FOR EACH ROW
BEGIN
  :new.creation_date := SYSDATE;
  :new.created_by := USER;
  :new.last_update_date := SYSDATE;
  :new.last_updated_by := USER;
END;

CREATE OR REPLACE TRIGGER parts_before_update
  BEFORE UPDATE on parts
  FOR EACH ROW
BEGIN
  :new.last_update_date := SYSDATE;
  :new.last_updated_by := USER;
END;

INSERT语句中,您将省略这四列并让触发器填入值。例如(显然,你的主要密钥来自类似序列而不是硬编码)

SQL>  insert into parts( pno, pname, qoh, price, reorder_level )
  2     values( 1, 'Widget', 10, 100, 75 );

1 row created.

SQL> select *
  2    from parts;

       PNO PNAME                                                     QOH
---------- -------------------------------------------------- ----------
     PRICE REORDER_LEVEL CREATION_ CREATED_BY LAST_UPDA LAST_UPDAT
---------- ------------- --------- ---------- --------- ----------
         1 Widget                                                     10
       100            75 26-SEP-12 SCOTT      26-SEP-12 SCOTT

答案 1 :(得分:3)

在您的案例中,无需在触发器中执行UPDATE语句。只需使用:new为列分配新值即可。

CREATE OR REPLACE TRIGGER pchange
  BEFORE UPDATE on parts
  FOR EACH ROW
BEGIN
  :new.last_update_date = SYSDATE;
  :new.last_updated_by = USER;
END;