CREATE OR REPLACE TRI_A1_INS 
AFTER INSERT OR DELETE OR UPDATE ON A1 
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO A2 SELECT * FROM :NEW;
END IF;
IF DELETING THEN
DELETE FROM A2 WHERE ID = :OLD.ID ;
END IF;
IF UPDATING THEN
UPATE A2 SET ID = :NEW.ID , NAME = :NEW.NAME WHERE ID = :OLD.ID ;
END IF;
END
我想知道:NEW,:OLD这两个变量是怎么获得的?因为本人才刚始接触oracle。然后能否再顺便说下oracle中的package  and package body两者的区别和用途。  THX!

解决方案 »

  1.   


    oracle trigger基本语法
      

  2.   

    A package is a group of related procedures and functions, along with the cursors and variables they use, stored together in the database for continued use as a unit. Similar to standalone procedures and functions, packaged procedures and functions can be called explicitly by applications or users.Oracle supplies many PL/SQL packages with the Oracle database server to extend database functionality and provide PL/SQL access to SQL features. For example, the ULT_HTTP supplied package enables HTTP callouts from PL/SQL and SQL to access data on the Internet or to call Oracle Web Server Cartridges. You can use the supplied packages when creating your applications or for ideas in creating your own stored procedures.You create a package in two parts: the specification and the body. The package specification declares all public constructs of the package and the body defines all constructs (public and private) of the package. This separation of the two parts provides the following advantages:You have more flexibility in the development cycle. You can create specifications and reference public procedures without actually creating the package body.You can alter procedure bodies contained within the package body separately from their publicly declared specifications in the package specification. As long as the procedure specification does not change, objects that reference the altered procedures of the package are never ed invalid. That is, they are never ed as needing recompilation.
      

  3.   

    :NEW,:OLD 为改变后前记录的值 ,ORACLE自动获得其它的基本概念google,哪里不懂再问
      

  4.   

    CREATE OR REPLACE TRIGGER TRI_A1_INS
      AFTER INSERT OR DELETE OR UPDATE ON A1 --在插入,删除,更新后触发(基于表A1)
      FOR EACH ROW --每一行都触发
    BEGIN
      
      IF INSERTING THEN --如果是插入(谓词INSERTING如果是插入则为TRUE)
        INSERT INTO A2
          SELECT :NEW.ID,:NEW.NAME FROM dual; --:new修饰符用来引用列的新值
      END IF;
      
      IF DELETING THEN --如果是删除(谓词DELETING如果是插入则为TRUE)
        DELETE FROM A2 WHERE ID = :OLD.ID; --:old修饰符用来引用列的旧值
      END IF;
      
      IF UPDATING THEN --如果是更新(谓词UPDATING如果是插入则为TRUE)
        UPATE A2 SET ID = :NEW.ID, NAME = :NEW.NAME WHERE ID = :OLD.ID;
      END IF;
    END
    /
    package定义包头,主要是包的说明部分,全局变量、类型、过程与函数说明。
    pageage body定义包体,主要是包的执行部分,定义过程与函数的实现部分。
      

  5.   

     IF INSERTING THEN --如果是插入(谓词INSERTING如果是插入则为TRUE)
     IF DELETING THEN --如果是删除(谓词DELETING如果是删除则为TRUE) 
     IF UPDATING THEN --如果是更新(谓词UPDATING如果是更新则为TRUE)
      

  6.   

    create or replace trigger emp_bonus_tri
    after insert or update or delete on emp
    for each row 
    begin
    if INSERTING  then
    insert into bonus(ENAME,JOB,SAL,COMM,OP)
    SELECT :NEW.ENAME,:NEW.JOB,:NEW.SAL,:NEW.COMM,'INSERT' FROM DUAL;
    END IF;
    IF UPDATING  THEN
    INSERT INTO BONUS(ENAME,JOB,SAL,COMM,OP)
    SELECT :OLD.ENAME,:OLD.JOB,:OLD,SAL,:OLD.COMM,'UPDATE' FROM DUAL;
    END IF;
    IF DELETING  THEN
    INSERT INTO BONUS(ENAME,JOB,SAL,COMM,OP)
    SELECT :OLD.ENAME,:OLD.JOB,:OLD,SAL,:OLD.COMM,'DELETE' FROM DUAL;
    END IF;
    end emp_bonus_tri;
     那我这个触发器又是哪里有错呢。 我真的找不出来呀。 帮忙看看
      

  7.   

    能把你的错误信息贴出来吗?还有,你的bonus表里有主键吗?你再插入的时候没有给主键赋值,是不是这个原因,可以考虑考虑?
      

  8.   

    错误信息:“ORA-04098:触发器'SCOTT.EMP_BONUS_TRI'无效且未通过重新验证”
    这是我在执行delete一条记录的时候报的。。bonus里面没有主键。。它就只有四个字段。
      

  9.   

    我靠,兄弟你也太不认真仔细了 :OLD,SAL 中间的.号写成 顿号了