我是一名大学生,有一份到期作业,我不知道如何解决这个问题。我尝试创建一个触发器,以便在患者插入/删除到PATIENT表之后/之后,将值(在IF语句中)插入到新表patient_log中。
尝试:
1.我的INSERT语句有效(我从trigger和if语句中单独测试了它)。
我还尝试了“旧的参考作为新的参考”
3.我试图在“NEW”和“OLD”前添加“:”,但SQL会弹出错误引用的错误消息。
****请保持编码尽可能简单。换句话说,代码应该就像你正在学习程序和触发器的基础知识(我希望这是有道理的)。
set pagesize 100
set linesize 130
set echo on
CREATE OR REPLACE TRIGGER after_pat AFTER INSERT OR DELETE ON patient
FOR EACH ROW
DECLARE
pat_name1 VARCHAR(41);
BEGIN
SELECT pat_name INTO pat_name1 FROM patient
WHERE patient_ID = :NEW.patient_ID OR patient_ID = :OLD.patient_ID;
IF inserting THEN
INSERT INTO patient_log
VALUES('New patient '||NEW.pat_name1||' inserted at ', TO_CHAR(sysdate,'Month DD, YYYY HH:MI PM'));
END IF;
IF deleting THEN
INSERT INTO patient_log
VALUES('Patient '||OLD.pat_name1||' released at ',TO_CHAR(sysdate,'Month DD, YYYY HH:MI PM'));
END IF;
END after_pat;
/
__________________________________________________________________
>ERROR CODES I get:
>It's for the IF statements i.e. "IF inserting then" & "IF deleting then"
>
>LINE/COL ERROR
>-------- -----------------------------------------------------------------
>9/2 PL/SQL: SQL Statement ignored<BR>
>10/29 PL/SQL: ORA-00984: column not allowed here
>14/2 PL/SQL: SQL Statement ignored<BR>
>15/26 PL/SQL: ORA-00984: column not allowed here
SQL>
______________________________________________________
> SQL> desc patient_log;
Name Type
**MESSAGE** VARCHAR2(50)
**TIME1** VARCHAR2(30)
答案 0 :(得分:0)
首先,将您的SELECT移到if inserting/deleting
内,并相应地进行调整: 您为旧记录搜索新记录或,而不是两者同时进行。
然后在INSERT INTO日志中使用变量pat_name1
。发生错误的原因是,您使用的NEW.pat_name1
和OLD.pat_name1
代替变量而不是
这样的事情:
CREATE OR REPLACE TRIGGER after_pat AFTER INSERT OR DELETE ON patient
FOR EACH ROW
DECLARE
pat_name1 VARCHAR(41);
BEGIN
IF INSERTING THEN
SELECT pat_name INTO pat_name1 FROM patient WHERE patient_ID = :NEW.patient_ID;
INSERT INTO patient_log VALUES('New patient ' || pat_name1 || ' inserted at ', TO_CHAR(sysdate,'Month DD, YYYY HH:MI PM'));
END IF;
IF DELETING THEN
SELECT pat_name INTO pat_name1 FROM patient WHERE patient_ID = :OLD.patient_ID;
INSERT INTO patient_log VALUES('Patient ' || pat_name1 || ' released at ',TO_CHAR(sysdate,'Month DD, YYYY HH:MI PM'));
END IF;
END after_pat;
/