0

首先也是最重要的,这是一个作业,所以大多数你想要建议的非常酷的预写功能,我都不会被允许使用。

我有几个表都有字段

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;
/
4

2 回答 2

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
于 2012-09-26T17:06:09.403 回答
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;
于 2012-09-26T17:05:24.147 回答