PLSQL使用if语句触发

时间:2013-01-23 07:18:57

标签: sql plsql triggers sql-update

我是PL SQL的新手,我正在尝试编写一个触发器,当表A中的字段更新时会触发,然后再更新表B中的字段。

第二个表可能没有任何相关记录,或者它可能有很多行(在这种情况下,我需要更新所有行)。

根据表A中更新的值(称为HNCMA.AGREEMENT_EOI.STATUS),我想影响表B中更新的内容(称为LMD.LM_ACTIVITY_.ACTIVITY_STATUS_CODE)。

这是我到目前为止所得到的,但我收到了这个错误:

PLS-00103:遇到以下其中一项时遇到符号“=”:: =。(@%;

任何帮助都会非常感激!!!!

由于


CREATE OR REPLACE TRIGGER TR_UPDATE_STATUS

AFTER UPDATE OF STATUS ON HNCMA.AGREEMENTS_EOI
FOR EACH ROW 

DECLARE

VarStatus Varchar(10);
VarStatusCode Int;

BEGIN

if :new.Status = 'Proposed' then VarStatus = 2 else
if :new.Status = 'Recommended' then VarStatus = 5 else
if :new.Status = 'Funded' then VarStatus = 5 else
if :new.Status = 'Completed' then VarStatus = 6 else
if :new.Status = 'Withdrawn' then VarStatus = 34 
end if

UPDATE
(SELECT LMD.LM_ACTIVITY.ACTIVITY_STATUS_CODE
FROM (HNCMA.AGREEMENTS_EOI INNER JOIN HNCMA.PROJECT ON HNCMA.AGREEMENTS_EOI.PROJECTID = HNCMA.PROJECT.PROJECTID) INNER JOIN (LMD.LM_POLYGON_PROJECT_REL INNER JOIN LMD.LM_ACTIVITY ON LMD.LM_POLYGON_PROJECT_REL.LMID = LMD.LM_ACTIVITY.LMID) ON HNCMA.AGREEMENTS_EOI.CASENO = LMD.LM_POLYGON_PROJECT_REL.LOCAL_PROJECT_NAME
WHERE (((HNCMA.PROJECT.CATCHMENT)='Murray') AND ((HNCMA.AGREEMENTS_EOI.CASENO)= :old.CASENO ));) s
Set s.ACTIVITY_STATUS_CODE = varstatuscode


End;

更新 *

感谢@heaps的帮助。我修改了我的代码以包含CASE函数并修复了我的格式错误。这是我到目前为止所做的(当我拿出更新语句时它可以正常工作),但我现在得到的“SQL命令未正确结束”错误...

帮助!?!

创建或替换TRIGGER TR_UPDATE_LMDB_STATUS1

AFTER UPDATE OF STATUS ON HNCMA.AGREEMENTS_EOI
FOR EACH ROW 

DECLARE

VarStatus Varchar(50);
VarStatusCode Int;

BEGIN

VarStatus := :new.status;

CASE varstatus

WHEN 'Proposed' THEN VarstatusCode := 2;
WHEN 'Recommended' THEN VarStatusCode := 5;
WHEN 'Funded' THEN VarStatusCode := 5;
WHEN 'Completed' THEN VarStatusCode := 6;
WHEN 'Withdrawn' THEN VarStatusCode := 34;

END CASE;

UPDATE LMD.LM_ACTIVITY
SET ACTIVITY_STATUS_CODE = VarStatusCode
FROM (HNCMA.AGREEMENTS_EOI INNER JOIN HNCMA.PROJECT ON HNCMA.AGREEMENTS_EOI.PROJECTID = HNCMA.PROJECT.PROJECTID) INNER JOIN (LMD.LM_POLYGON_PROJECT_REL INNER JOIN LMD.LM_ACTIVITY ON LMD.LM_POLYGON_PROJECT_REL.LMID = LMD.LM_ACTIVITY.LMID) ON HNCMA.AGREEMENTS_EOI.CASENO = LMD.LM_POLYGON_PROJECT_REL.LOCAL_PROJECT_NAME
WHERE (((HNCMA.PROJECT.CATCHMENT)='Murray') AND ((HNCMA.AGREEMENTS_EOI.CASENO)= :old.CASENO )) ;

End;

1 个答案:

答案 0 :(得分:1)

更换 varstatus = 2 Varstatus:=2 如果;,请加上分号 然后再试一次

已修改 ..以获取更新后的问题>>

错误SQL command not properly ended是因为您应该使用UPDATE命令

进行检查

更新中似乎有问题FROM子句, 因为我认为更好的尝试  UPDATE... WHERE EXISTS..MERGE命令,oracle在UPDATE中不支持FROM子句,而是可以将其用作查询