从实体 table 插入关系 table

inserting into relational table from entity table

你好,我想创建一个触发器,它会在将值插入到实体 [=38= 之后,在关系 table A_EQ、A_U 和 A_P 上插入值] 账户。如果只有 ACCOUNTS.TYPE='Equipments',值将插入到 A_EQ,如果只有 ACCOUNTS.TYPE='Utility',则值将插入到 A_U,如果只有 A_P,则值将插入到 [=34] =] 剩下的。 每当我尝试编译触发器时,都会显示某些错误:

Error(40,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(41,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(42,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(45,9): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(46,13): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(47,13): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(50,9): PLS-00049: bad bind variable 'NEW.A_P'
Error(51,13): PLS-00049: bad bind variable 'NEW.A_P'
Error(52,13): PLS-00049: bad bind variable 'NEW.A_P'
Error(57,4): PLS-00103: Encountered the symbol ";" when expecting one of the following:     if 

触发代码如下:

create or replace trigger accounts_relational_insert
 after insert   on accounts

REFERENCING NEW AS new OLD AS Old


for each row 
declare
sys_month VARCHAR2 (10) ;
i_month VARCHAR2 (10);
old1 int;
curr_amount number (20,2);
curr_id varchar2 (20);
curr_txn varchar2 (20);
curr_type varchar2 (20);
begin
select to_char(SYSDATE,'Month')  into sys_month from dual;
select ACCOUNTS_ID_SEQ.currval into old1 from dual;
select to_char(paid_on,'Month') into i_month from accounts where  transaction_id ='Txn' || lpad(old1,9,'0');
select amount  into curr_amount from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select  id into curr_id from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select transaction_id into curr_txn from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select type into curr_type from accounts where transaction_id='Txn' || lpad(old1,9,'0');
if i_month = sys_month then
    if curr_type ='Utility' then 
            :new.a_u.transaction_id := curr_txn;
            :new.a_u.u_id :=curr_id;
            :new.a_u.amount := curr_amount;
            
    else if curr_type='Equipments' then
        :new.a_eq.transaction_id := curr_txn;
            :new.a_eq.eq_id :=curr_id;
            :new.a_eq.amount := curr_amount;
     else
     
        :new.a_p.transaction_id := curr_txn;
            :new.a_p.barcode:=curr_id;
            :new.a_p.amount := curr_amount;
            
    end if;
end if;

end;

表格如下:

如果有人告诉我如何解决此错误,以及是否有任何其他方法可以在插入帐户 table 后插入关系 tables,这将非常有帮助。

您可以像这个一样简单地重新排列触发器

CREATE OR REPLACE TRIGGER accounts_relational_insert
  AFTER INSERT ON accounts
  FOR EACH ROW
BEGIN
  IF TO_CHAR(:new.paid_on, 'YYYYMM') = TO_CHAR(sysdate, 'YYYYMM') THEN
    IF    :new.type = 'Utility' THEN
      INSERT INTO a_u VALUES(:new.id, :new.transaction_id, :new.amount);
    ELSIF :new.type = 'Equipments' THEN
      INSERT INTO a_eq VALUES(:new.id, :new.transaction_id, :new.amount);    
    ELSE
      INSERT INTO a_p VALUES(:new.id, :new.transaction_id, :new.amount);
    END IF;
  END IF;
END;
/

哪里

  • 已经可以从的标识符中得到的值 table accounts 符合 :new 的列,是 插入的不应该 return 来自 SELECT 语句。此外,你会发生变异 这种情况的触发错误。

  • 确实不需要定义局部变量 上面表达的,以及 i_monthsys_month 的值 将直接派生自 TO_CHAR(:new.paid_on, 'YYYYMM')TO_CHAR(sysdate, 'YYYYMM') 分别。顺便说一句,不喜欢使用字符串 字符转换中的表达式,例如 Month,因为您可能不会 只面临区分大小写的问题,但国际化也可能失败 与语言差异相关的研究。

  • 触发器是在 accounts table 上为其他 table 创建的 应该使用 INSERT 语句,不能使用这样的变量赋值。

  • 表达式REFERENCING NEW AS new OLD AS old是多余的,因为 默认。