【问题标题】:Update value of other column on update of this column更新此列时更新其他列的值
【发布时间】:2019-01-20 09:07:33
【问题描述】:

我有疑问,如何在 Intersystems Cache 中执行触发器之类的操作。 情况: 例如,我有带有属性(列)的表 X 值A,值B 我想在 UPDATE 更改 valueA 时更新 valueB。我已经定义了全局变量 ^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 时起作用,有什么想法吗? 附言抱歉英语不好

【问题讨论】:

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


    【解决方案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)
               }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      相关资源
      最近更新 更多