在更新此列时更新另一列的值

时间:2018-08-13 15:22:04

标签: intersystems-cache intersystems intersystems-cache-studio

我有疑问,如何在系统间缓存中执行类似触发器的操作。 情况: 例如我有带有属性(表)的表X 值A,值B 我想在值A通过UPDATE更改时更新值B。我定义了全局变量^ VALUEBGENER并使用它来增加$ SEQ函数, 我的想法是:

Class User.X Extends %Persistent [ ClassType = persistent, DdlAllowed, Final, Owner = {_SYSTEM}, ProcedureBlock, SqlRowIdPrivate, SqlTableName = X]
{
    Property VALUEA As %Library.String(MAXLEN = 8) [ Required,SqlColumnumber = 1];
    Property VALUEB As %Library.Integer(MAXVAL = 2147483647, MINVAL = -2147483648) [ Required,SqlColumnNumber = 1,SqlComputed,SqlColumnumber = 2, SqlComputeCode = {SET {valueB}=$SEQ(^VALUEBGENER)}, SqlComputeOnChange = %%UPDATE];
}

但是当我更改valuea时它不起作用,但是当我更改valueb时它起作用,所以有什么想法吗? 附言对不起,英语不好

1 个答案:

答案 0 :(得分:1)

可以通过添加触发器和SqlCompute来实现


    Class User.X Extends %Persistent [ ClassType = persistent, DdlAllowed, Final, Owner = {_SYSTEM}, ProcedureBlock, SqlRowIdPrivate, SqlTableName = X]
    {
        Property VALUEA As %Library.String(MAXLEN = 8) [ Required, SqlComputed,SqlColumnumber = 1];
        Property VALUEB As %Library.Integer(MAXVAL = 2147483647, MINVAL = -2147483648) [ Required,InitialExpression=0,SqlColumnNumber = 2, SqlComputeCode = {SET {*}=$SEQ(^VALUEB)}, SqlComputeOnChange = %%UPDATE ];
        Trigger[Event=Update]{
           NEW valuebx
           new rowid
           set rowid={ID}
           SET valuebx= 0
           // {fieldname*C} evaluates to 1 if the field has been changed and 0 
           if({VALUEA*C}=1){
             // we trigger sql computeCode and inc of global variable by update
             //and it doesnt matter what is here in :valuebx
               &sql(update x set valueb=:valuebx WHERE ROWID=:rowid)
           }
        }
    }