触发器中的Oracle to_char子查询

时间:2013-05-01 00:24:00

标签: sql triggers oracle10g subquery to-char

我有一个表(会议),其中包含日期类型属性(MeetDate)和另一个varchar2类型属性(WorkWeek)。我正在尝试使用to_char函数执行After触发器以基于MeetDate值填充WorkWeek字段。分别尝试以下代码并且它们编译时没有错误,但是当我尝试插入Null for WorkWeek的行时,它给了我一个“变异触发器/函数可能看不到它”的错误。我在这做错了什么?提前感谢任何帮助。

--Code 1
Create or Replace Trigger Update_WorkWeek
After Insert On Meeting
For Each Row
Begin
Update Meeting
Set WorkWeek  = (Select to_char(:new.MeetDate, 'YYYY IW') From Dual)
Where MeetID = :new.MeetID;
End;
/
show Errors;

--Code 2
Create or Replace Trigger Update_WorkWeek
After Insert On Meeting
For Each Row
Begin
if :New.WorkWeek is Null then
Update Meeting
Set WorkWeek  = (Select to_char(:new.MeetDate, 'YYYY IW') From Dual)
Where MeetID = :new.MeetID;
End if;
End;
/
show Errors;

1 个答案:

答案 0 :(得分:1)

您只需要一个触发器来在插入之前更改列的值 - 并且它位于同一行,因此您不需要更新:

Create or Replace Trigger Update_WorkWeek
BEFORE Insert On Meeting
For Each Row
Begin
  :new.WorkWeek := to_char(:new.MeetDate, 'YYYY IW');
End;
/
show Errors;

如果更改了MeetDate,您可能希望该列保持最新,即:

Create or Replace Trigger Update_WorkWeek
BEFORE Insert
    OR Update OF MeetDate
On Meeting
For Each Row
Begin
  :new.WorkWeek := to_char(:new.MeetDate, 'YYYY IW');
End;
/
show Errors;