简体   繁体   English

如何将if语句在Informix中触发

[英]How to put if statement in Informix triggers in before

I need to make a trigger that won't allow update of "grade" where grade is 1. So, I need to make it with before the triggering statement. 我需要创建一个触发器,不允许在1级为1的情况下更新“ grade”。因此,我需要在触发语句before使用它。 Procedure returnMistake() is not of a concern - it just raises exception . 过程returnMistake() -它仅引发exception

I have the following code: 我有以下代码:

CREATE TRIGGER changeGrade
    UPDATE OF grade ON subject
    BEFORE (
        IF (grade = 1) EXECUTE PROCEDURE returnMistake() END IF; 
    )

I know that I don't specify anywhere value of the grade , but just name of the column, but I can't put REFERENCING OLD because I'm using BEFORE and not FOR EACH ROW . 我知道我没有指定grade任何值,而只是列的名称,但是我不能放REFERENCING OLD因为我使用的是BEFORE而不是FOR EACH ROW

How can I make this? 我该怎么做? This stops at IF statement describing syntax error. 这在描述语法错误的IF语句处停止。 For the matter of example, I'll put here code that is passing in Informix: 就示例而言,我将在此处放入Informix中传递的代码:

CREATE TRIGGER changeGrade
        UPDATE OF grade ON subject
        BEFORE (
            EXECUTE PROCEDURE returnMistake() 
        )

How to do it with BEFORE ? 如何使用做BEFORE I would know how to make it with FOR EACH ROW - I would just let it to be changed, and then if it was 1, change it back to the previous and calling the returnMistake() , although I think I'm supposed to use BEFORE . 我会知道如何使用FOR EACH ROW我只希望对其进行更改,然后将其更改为1,然后将其更改回前一个并调用returnMistake() ,尽管我认为我应该使用BEFORE

Unfortunately Informix (at current version , v12.10) don't support embedded SPL statements into a trigger "body", which make little annoying include some logical at triggers since you are obligated to create a SPL to do the job... 不幸的是,Informix(当前版本为v12.10)不支持在触发器“ body”中嵌入嵌入式SPL语句,因为您必须创建一个SPL来完成这项工作,所以在触发器上包含一些逻辑上的内容并不会令人讨厌。
For more simplistic logical or filter you can use the WHEN option. 对于更简单的逻辑或过滤器,可以使用WHEN选项。

That said, your solution don't appear much logical for me... or you are misinterpreted the concept of before, for each row and after actions from a trigger. 就是说,您的解决方案对我而言似乎并不太合逻辑……或者您误解了触发器before, for each row and after的概念。

What I means is: If you use the before action, the trigger will be called only once before of the UPDATE statement (no matter if this update will affect one, non or lot of rows). 我的意思是:如果使用before动作,则该触发器将仅在UPDATE语句之前调用一次(无论此更新是否会影响一个,一个或多个行)。
If the before trigger was executed only once and (most important) before the statement start running, how they will able to know the content of the "grade" field for all rows which could be affected? 如果before触发器仅在语句开始运行之前 (并且最重要) 执行一次,那么他们将如何知道所有可能受影响的行的“ grade”字段的内容?

So, if you want to create a exception if the row with "grade" content equal 1 , you are without options and need use the for each row + when + stored procedure + raise exception ... 因此,如果您要在“成绩”内容的行等于1的情况下创建异常,则没有选项,需要for each row + when +存储过程+引发异常使用...

If your database is logged (have transaction control) and this table is standard (not a RAW table) the raised exception will abort and rollback all rows affected by this update statement. 如果您的数据库已记录(具有事务控制)并且该表是standard表(不是RAW表),则引发的异常将中止并回滚受此update语句影响的所有行。 This way you will reach your objective. 这样,您将达到目标。

CREATE TRIGGER changeGrade
UPDATE OF grade ON subject
REFERECING OLD AS O
WHEN ( o.grade =1 )   
FOR EACH ROW ( EXECUTE PROCEDURE returnMistake() )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM