在主表的数据库触发器中将数据插入到临时表中

时间:2017-08-18 18:52:52

标签: sql oracle plsql triggers

我有以下要求 -

我有2个表主表A,临时表B

我在主表A上写了一个插入触发器来检查一些错误的数据。如果数据错误,则它将插入到临时表B中,以便用户可以查看错误并更正然后再次上传数据。 我的问题是-Data没有插入到临时表中,因为我在插入语句后使用了raise_form_trigger_failure。还有其他方法可以做到这一点吗?

TRIGGER CODE -

create or replace trigger CCM_TEST 
   before insert OR UPDATE ON "CCM_MANAGER" 
   for each row 
    declare
  l_user varchar2(500);
  v_user number;
  v_cost_centre number;
  v_company_code number;
  v_show_error varchar2(100);
begin  
l_user := NVL(v('APP_USER'), user);
   if inserting then 

           begin 
            select count(1)
            into v_cost_centre
            From gl_code_combinations
            where segment2=:new.cost_center
            and enabled_flag ='Y';

            if (v_cost_centre= 0) then    
             v_show_error:='Cost Centre does not exists!!  '||'Cost Centre -'||:new.cost_center;      
             :new.CHECK_INSERT_FLAG:='Y';

              Insert into CCM_MANAGER_STG(COMPANY_CODE,
              COST_CENTER,
              USER_NAME,
              EFFECTIVE_START_DATE,
              EFFECTIVE_END_DATE,
              CREATED_BY,
              CREATION_DATE,
              LAST_UPDATED_BY,
              LAST_UPDATE_DATE,
              LINE_ID,
              CHECK_INSERT_FLAG,
              SHOW_ERRORS) values
              ( :new.COMPANY_CODE,
              :new.COST_CENTER,
              :new.USER_NAME,
              :new.EFFECTIVE_START_DATE,
              :new.EFFECTIVE_END_DATE,
              :new.CREATED_BY,
              :new.CREATION_DATE,
              :new.LAST_UPDATED_BY,
              :new.LAST_UPDATE_DATE,
              :new.LINE_ID,
              :new.CHECK_INSERT_FLAG,
              v_show_error);
              end if;
            raise_application_error (-20001, 'Cost Centre does not exists!!  '||'Cost Centre -'||:new.cost_center||' ');                              
          exception
            when others then
            --raise_application_error (-20001, sqlerrm);
            null;

      end;

       begin 
            select count(1)
            into v_company_code
            From gl_code_combinations
            where segment1=:new.company_code
            and enabled_flag ='Y';

            if (v_company_code= 0) then   

             v_show_error:='Company Code does not exists!!'; 
             :new.CHECK_INSERT_FLAG:='Y';

              Insert into CCM_MANAGER_STG(COMPANY_CODE,
              COST_CENTER,
              USER_NAME,
              EFFECTIVE_START_DATE,
              EFFECTIVE_END_DATE,
              CREATED_BY,
              CREATION_DATE,
              LAST_UPDATED_BY,
              LAST_UPDATE_DATE,
              LINE_ID,
              CHECK_INSERT_FLAG,
              SHOW_ERRORS) values
              ( :new.COMPANY_CODE,
              :new.COST_CENTER,
              :new.USER_NAME,
              :new.EFFECTIVE_START_DATE,
              :new.EFFECTIVE_END_DATE,
              :new.CREATED_BY,
              :new.CREATION_DATE,
              :new.LAST_UPDATED_BY,
              :new.LAST_UPDATE_DATE,
              :new.LINE_ID,
              :new.CHECK_INSERT_FLAG,
              v_show_error);
           end if;

               raise_application_error (-20001, 'Company Code does not exists!!  '||'Company code -'||:new.company_code||' ');
             --  Rollback;
           exception
            when others then
            raise_application_error (-20001, sqlerrm);
            null;
          end;
     end if; 

end;

1 个答案:

答案 0 :(得分:0)

第一种方式是autonomous transaction

第二种方式是compound trigger