如何使用带有多个表的CREATE VIEW对象在Oracle中使用INSTEAD OF触发器来插入数据?

时间:2013-08-24 22:21:40

标签: oracle

根据stackoverflow用户的反馈,我研究了docs.oracle.com关于INSTEAD OF tirggers,但我仍然无法做到这一点。视图创建正常,但INSTEAD OF触发器没有。而且,我不明白如何为多个输入设置'n'。执行代码后,没有任何错误迹象或是否有效。我松开了SQL>提示,不能做任何事情,最终不得不重新启动环境。

以下是我根据Oracle的示例提出的建议:

SQL> CREATE OR REPLACE VIEW cust_order AS
  2   SELECT book_customer.customerid, lastname, state, book_order.orderid, orderdate
  3    FROM book_customer
  4      JOIN book_order ON book_order.customerid = book_customer.customerid
  5  ;

View created.

SQL> 
SQL>
SQL> CREATE OR REPLACE TRIGGER cust_order_insert
  2   INSTEAD OF INSERT ON cust_order
  3    REFERENCING NEW AS n
  4     FOR EACH ROW
  5  
  6  DECLARE
  7   rowcnt number;
  8  
  9  BEGIN
 10   SELECT COUNT(*) INTO rowcnt
 11    FROM book_customer
 12     WHERE book_customer.customerid = :n.book_customer.customerid;
 13      IF rowcnt = 0 THEN
 14       INSERT INTO book_customer(customerid, lastname, state)
 15        VALUES (:n.book_customer.customerid, :n.lastname, :n.state);
 16         ELSE
 17         UPDATE book_customer SET book_customer.customerid = :n.customerid
 18          WHERE book_customer.customerid = :n.customerid;
 19       END IF;
 20       
 21   SELECT COUNT(*) INTO rowcnt
 22    FROM book_order
 23     WHERE book_order.orderid = :n.orderid;
 24      IF rowcnt = 0 THEN
 25       INSERT INTO book_order(orderid, orderdate)
 26        VALUES (:n.book_order.orderid, :n.orderdate);
 27         ELSE
 28         UPDATE book_order SET book_order.orderid = :n.orderid
 29          WHERE book_order.orderdate = :n.orderdate;
 30       END IF;
 31  END;
 32  
 33  
 34  
 35  
 36  --SQL> prompt won't come back.
 37  
 38  
 39  
 40  

以下是表格说明:

SQL> desc cust_order;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- ------------------------
 CUSTOMERID                                                        NOT NULL NUMBER(4)
 LASTNAME                                                                   VARCHAR2(10)
 STATE                                                                      VARCHAR2(2)
 ORDERID                                                           NOT NULL NUMBER(4)
 ORDERDATE                                                                  DATE

说明:

Create table Book_customer
(CustomerID  NUMBER(4) CONSTRAINT PK_BOOKCUSTOMER PRIMARY KEY,
LastName VARCHAR2(10),
FirstName VARCHAR2(10),
Address VARCHAR2(20),
City VARCHAR2(20),
State VARCHAR2(2),
Zip VARCHAR2(5),
Referred NUMBER(4));

Create Table Book_order
(OrderID NUMBER(4) CONSTRAINT PK_BOOKORDER_ORDERID PRIMARY KEY,
CustomerID NUMBER(4) CONSTRAINT FK_BookOrder_BookCustomer REFERENCES Book_Customer (CustomerID),
OrderDate DATE,
ShipDate DATE,
ShipStreet VARCHAR2(20),
ShipCity VARCHAR2(20),
ShipState VARCHAR2(2),
ShipZip VARCHAR2(5));

任何想法或建设性批评都表示赞赏。

2 个答案:

答案 0 :(得分:0)

我不会依赖第三方书籍,因为它在Oracle文档中有所描述:http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/triggers.htm#i1025919http://docs.oracle.com/cd/B28359_01/server.111/b28310/views001.htm#ADMIN11782

密切关注 Key-Preserved Tables 的含义。在您的情况下,假设Book_Customer.Customer是主/唯一键,您应该能够插入Book_Customer,但不能插入Cust_Order

答案 1 :(得分:0)

我想你还没有正确地结束PL / SQL块。最后只需使用'/'。您还可以在同一个IF-ELSE块下的两个表中包含DML。

SQL> CREATE OR REPLACE TRIGGER cust_order_insert
  2   INSTEAD OF INSERT ON cust_order
  3    REFERENCING NEW AS n
  4     FOR EACH ROW
  5  
  6  DECLARE
  7   rowcnt number;
  8  
  9  BEGIN
 10   SELECT COUNT(*) INTO rowcnt
 11    FROM book_customer
 12     WHERE book_customer.customerid = :n.book_customer.customerid;
 13      IF rowcnt = 0 THEN
 14       INSERT INTO book_customer(customerid, lastname, state)
 15        VALUES (:n.book_customer.customerid, :n.lastname, :n.state);
 16       INSERT INTO book_order(orderid, orderdate)
 17        VALUES (:n.book_order.orderid, :n.orderdate);
 18         ELSE
 19         UPDATE book_customer SET book_customer.customerid = :n.customerid
 20          WHERE book_customer.customerid = :n.customerid;
 21         UPDATE book_order SET book_order.orderid = :n.orderid
 22          WHERE book_order.orderdate = :n.orderdate;
 23       END IF;
 24  END;
 25 /