2011-12-14 56 views
0

为什么这给我错误?警告:使用编译错误创建的过程?

CREATE OR REPLACE trigger customerLineCount 
BEFORE insert on cust_line 
for each row 
when(new.cust_id > 0) 
DECLARE 
    lineCount number; 
BEGIN 
    select count (*) into lineCount 
    from (cust_line inner join customer 
    on cust_line.cust_id = customer.cust_id) 
    where (customer.cust_id = :new.cust_id) 

    if :new.gender = "m" and lineCount = 3 THEN 
     dbms_output.put_line ('Error! User already has 3 lines'); 
    elseif :new.gender = "f" and lineCount = 1 THEN 
     dbms_output.put_line ('Error! User already has 1 line'); 
    end if; 
END customerLineCount; 
/
+4

请运行`show errors`来查看编译错误消息是什么(或者从user_errors等中选择它) – derobert 2011-12-14 18:50:18

回答

3

在正文中首次选择后缺少分号。

5

1)PL/SQL中的字符串由单引号分隔,而不是双引号。所以,如果你想检查gender是什么,你需要像

if :new.gender = 'm' and lineCount = 3 THEN 
    dbms_output.put_line ('Error! User already has 3 lines'); 
elseif :new.gender = 'f' and lineCount = 1 THEN 
    dbms_output.put_line ('Error! User already has 1 line'); 
end if; 

2)你SELECT语句丢失在最后一个分号。

3)但是,一旦解决了编译错误,几乎肯定会遇到运行时错误。通常,表上的行级触发器不能查询同一个表。因此您在cust_line上的行级触发器无法查询cust_line表。您可以通过创建一个包含PL/SQL集合的包然后创建多个触发器来解决这个问题。 before语句触发器会初始化集合,行级触发器会使用新插入行中的键填充集合。然后,一个after语句触发器将遍历集合,查询表并应用所需的任何业务逻辑。然而,这是一个非常复杂的方法,在实践中很少需要。通常,使用约束而不是触发器或通过执行插入的存储过程中的业务规则可以更好地服务您。

+0

+1用于推荐使用触发器的约束 – 2011-12-15 10:14:56