我正在学习PL / SQL专门触发器,我想学习如何使用触发器对多个表进行操作,但我无法理解该过程。这是一个例子:
CREATE TABLE Sess
(code INTEGER NOT NULL,
dateStart DATE NOT NULL,
dateEnd DATE NOT NULL,
CONSTRAINT KeySess PRIMARY KEY(code)
)
CREATE TABLE Ins
(codeIns CHAR(12) NOT NULL,
code INTEGER NOT NULL,
dateIns DATE NOT NULL,
dateAb DATE,
note INTEGER,
CONSTRAINT KeyIns PRIMARY KEY (codeIns, code)
)
1 /我正在尝试构建一个能够确保这一点的触发器:
before Insert or Update Ins
dateAb is between dateStart and dateEnd
2 /我还希望ALTER TABLE
和锁定代码以及Ins中的codeIns,所以没有人可以更新它们但我不知道命令。
对于第一次触发,我尝试了类似的事情:
CREATE OR REPLACE TRIGGER test
BEFORE INSERT OR UPDATE OF dateAb ON Ins
FOR EACH ROW
DECLARE
start date;
BEGIN
SELECT DISTINCT s.dateStart INTO start
WHERE Ins.code = s.code
IF (:NEW.dateAb < start) THEN
raise_application_error(-20101,'dateAb incorrect');
END IF;
END;
注意:我没有声明变量end来检查它是否介于两者之间,我的目的是测试正确的语法
注2:表格为空。
我有Warning: Trigger created with compilation errors.
和3个错误:
PL/SQL: SQL Statement ignored
4/40 PL/SQL: ORA-00923: key-word FROM absent
9/5 PLS-00103: Encountered the symbol "IF" when expecting one of the
following:
;
最后,就像我说的那样,我不知道在表上锁定列以防止更新的正确语法,或者是否可能。
我正在学习,如果你们中的一个人可以用我的两个要求教会我正确的处理方法,我会赞成这一点。 谢谢
答案 0 :(得分:1)
试试这个:
create or replace trigger test
before insert or update of dateab on ins
for each row
declare
l_sess_start date;
begin
select s.datestart into l_sess_start
from sess s
where s.code = :new.code;
if :new.dateab < l_sess_start then
raise_application_error
( -20101
, 'Start date ' || to_char(:new.dateab,'DD-MON-YYYY') ||
' cannot be before ' || to_char(l_sess_start,'DD-MON-YYYY'));
end if;
end;
测试:
insert into sess values (1, date '2018-04-22', date '2018-04-30');
insert into ins values ('Z', 1, date '2018-04-01', date '2018-04-01', null);
ERROR at line 1:
ORA-20101: Start date 01-APR-2018 cannot be before 22-APR-2018
ORA-06512: at "WILLIAM.TEST", line 10
ORA-04088: error during execution of trigger 'WILLIAM.TEST'
答案 1 :(得分:0)
PLS-00103: Encountered the symbol "IF" when expecting one of the
following:
;
此错误是因为您在select语句
之后缺少;