2016-02-25 56 views
0

我试图取决于在现场从我的表1中更新的值我的表2更新领域,甲骨文更新触发成功与编译错误

create or replace trigger update_value 
AFTER update of field_1 on table_1 
for each row 
begin 
if(:New.field_1 = 'y') then 
update table_2 
set field_2 = 'updated' 
from table_1 
where table_1.id = table_2.id 

end if; 
end; 
+0

哪个RDBMS是这样的?触发器是**高度**特定于供应商的 - 请添加一个标签以指定您是使用'mysql','postgresql','sql-server','oracle'还是'db2' - 或者其他的东西。 –

+0

okey thanks让我知道 – thomaSmith

+0

在运行该命令之后执行'show errors',或者查询'user_errors',看看实际的底层错误是什么。 'table_1'应该做什么? –

回答

0

使用触发器如果一个触发器或任何其他存储的PL/SQL代码编译时会出现错误或警告,您可以通过执行show errors(如果您的客户端支持; SQL * Plus和SQL Developer)来看到实际问题。或者您可以查询适当的对象类型和名称,例如:

select * from user_errors where type = 'TRIGGER' and name = 'UPDATE_VALUE'; 

你会得到PLS-00103: Encountered the symbol ";" ...的代码user_errorsall_errors视图。更新语句后没有分号。如果添加,您将得到PL/SQL: ORA-00933: SQL command not properly ended,因为在更新语句中不能有from子句(除非有子查询)。

您不需要直接参考table_1;你已经使用了一个:NEW变量,你只需要使用另一个:

create or replace trigger update_value 
after update of field_1 on table_1 
for each row 
begin 
    if :NEW.field_1 = 'y' then 
    update table_2 
    set field_2 = 'updated' 
    where id = :NEW.id; 
    end if; 
end; 
/