SQL太多级别的触发器递归

时间:2015-01-14 12:56:06

标签: sqlite triggers

@ http://www.mediafire.com/download/5h674s7wdzk8tek/m4_2013.db 我正在使用sqlitestudiov3.01用于上述数据库。问题是它不会让编辑数据值和发起错误

An error occurred while commiting the data: too many levels of trigger recursion

如果我放下触发器,则会解决此错误。我的triiger如下

CREATE TRIGGER MACRO_A
       AFTER UPDATE ON ELZ_A
       FOR EACH ROW
BEGIN
    UPDATE ELZ_A
       SET CURRENT_DENSITY = ROUND( ( LOAD / 2.721 ) , 2 );

    UPDATE ELZ_A
       SET VOLTS_AVG = ROUND( ( VOLTS_T / ELEMENTS ) , 2 );

    UPDATE ELZ_A
       SET VOLTS_STNDR = ROUND( 2.4 +( ( 12.75 / 2.721 ) *( ( VOLTS_AVG - 2.4 ) / CURRENT_DENSITY )  ) -( ( 90 - CATHOLYTE_TEMP ) * 0.01 ) +( ( 32 - CATHOLYTE_CONC ) * 0.02 ) , 2 );

    UPDATE ELZ_A
       SET KF_FACTOR = ROUND( ( ( VOLTS_AVG -( 90 - CATHOLYTE_TEMP ) * 0.016 *( ( LOAD / 2.721 ) / 5 )  ) +( ( 32 - CATHOLYTE_CONC ) * 0.033 *( ( LOAD / 2.721 ) / 5 ) - 2.4 )  ) /( LOAD / 2.721 ) , 3 );

    UPDATE ELZ_A
       SET PRODUCTION = ROUND( 0.001492 * 24 * ELEMENTS * LOAD *( EFFICIENCY / 100 ) , 2 );

    UPDATE ELZ_A
       SET TEMP_CORRELATION = ROUND( 7 *( CURRENT_DENSITY / 3 ) *( 90 - CATHOLYTE_TEMP ) , 2 );

    UPDATE ELZ_A
       SET CONC_CORRELATION = ROUND( 14 *( CURRENT_DENSITY / 3 ) *( 32 - CATHOLYTE_CONC ) , 2 );

    UPDATE ELZ_A
       SET DC_POWER_PER_TON = ROUND( ( ( 24 * LOAD * VOLTS_T ) / PRODUCTION ) - TEMP_CORRELATION - CONC_CORRELATION, 0 );
END;

嗯,我不知道上面是否符合标准,但至少在某些应用中,它可以完美无缺地运行。模仿Excel公式计算。任何有想法的人

1 个答案:

答案 0 :(得分:3)

任何列更新时,会触发此触发器,因此它也会触发自身更新的列。

如果您区分"输入"和"输出"如果只有后者由触发器计算,则可以将触发器限制为仅在输入列上触发:

CREATE TRIGGER ...
AFTER UPDATE OF LOAD, VOLTS_T, ELEMENTS, ... ON ...

但是,将派生值存储在数据库中是一个坏主意。 最好使用视图来动态计算派生值:

CREATE VIEW ELZ_with_all_values AS
SELECT LOAD,
       VOLTS_T,
       ELEMENTS,
       ...
       LOAD / 2.721 AS CURRENT_DENSITY,
       VOLTS_T / ELEMENTS AS VOLTS_AVG,
       ...
FROM ELZ_A;

请注意,永远不要将ROUND()用于进一步计算的值。 (在Excel中,格式只会影响值的显示方式。)