我是第一次在SQL中编写触发器。任何人都可以告诉我,我在这段代码中写错了什么?
SQL> CREATE OR REPLACE TRIGGER Course_Allocation_After_Update
2 AFTER UPDATE
3 ON COURSE_ALLOCATION
4 FOR EACH ROW
5 DECLARE
6 v_user_id varchar2(20);
7 BEGIN
8 --Finding User who is performing the update in the table
9 SELECT USER
10 INTO v_user_id
11 FROM dual;
12 --Inserting Information Into Staging Table (Log Table)
13 IF UPDATING(STU_ID)
14 THEN
15 INSERT INTO STAGING_TABLE
16 (user_id,old_value,new_value,date_when_updated,time_when_udated)
17 VALUES
18 (v_user_id,:old.STU_ID,:new.STU_ID,sysdate,systime)
19 END IF;
20 END;
21 /
Warning: Trigger created with compilation errors.
这些是错误:
11/1 PL / SQL:忽略SQL语句
14/53 PL / SQL:ORA-00933:SQL命令未正确结束
16/4 PLS-00103:遇到符号“;”当期待其中一个
以下:
如果
答案 0 :(得分:2)
systime
- Oracle中不存在。 SYSDATE
- 已包含有关时间的信息。如果要分隔它,可以使用trunc(sysdate)
- 仅返回日期和to_char(sysdate,'HH24:MI:SS') - 随时间返回字符串。或者您的意思是systimestamp
我的触发器版本代码:
CREATE OR REPLACE TRIGGER Course_Allocation_After_Update
AFTER UPDATE
ON COURSE_ALLOCATION
FOR EACH ROW
DECLARE
v_user_id varchar2(20);
BEGIN
--Inserting Information Into Staging Table (Log Table)
IF UPDATING(STU_ID)
THEN
INSERT INTO STAGING_TABLE
(user_id,old_value,new_value,date_when_updated)
VALUES
(USER,:old.STU_ID,:new.STU_ID,sysdate);
END IF;
END;
/
答案 1 :(得分:1)
试试这个:
CREATE OR REPLACE TRIGGER Course_Allocation_After_Update
AFTER UPDATE ON COURSE_ALLOCATION
referencing old as old new as new
FOR EACH ROW
DECLARE
v_STU_ID varchar2(20);
BEGIN --Finding User who is performing the update in the table
SELECT STU_ID
INTO v_STU_ID
FROM COURSE_ALLOCATION;
--Inserting Information Into Staging Table (Log Table)
IF UPDATING('STU_ID')
THEN
INSERT INTO STAGING_TABLE (user_id,old_value,new_value,date_when_updated)
VALUES (USER,:old.v_STU_ID,:new.v_STU_ID,sysdate);
END IF;
END;
/
在我看来:
SELECT STU_ID
INTO v_STU_ID
FROM COURSE_ALLOCATION;
不能很好,因为您将从course_allocation表中选择所有stu_id记录。这会给你一个" too_many_rows"错误。 course_allocation表格如何?