各位好,本人oracle新手,现在写了个触发器实现数据导入时候自动传入参数调用存储过程,数据是通过perl采集过来的。现在有一个问题,在启用触发器以后数据不是insert而是update导致数据入库失败,下面是写的触发器,存储过程,perl模块,请高手帮忙分析下是哪个地方的问题,谢谢了。
触发器:
CREATE OR REPLACE TRIGGER TRG_S_PM_EQUIP_CPU
       before INSERT ON S_PM_EQUIP_CPU FOR EACH ROW
BEGIN
       PRO_S_PM_EQUIP_CPU(:NEW.STARTTIME,:NEW.EQUIPNAME);
END trg_S_PM_EQUIP_CPU;
存储过程:
CREATE OR REPLACE PROCEDURE "STWG"."PRO_S_PM_EQUIP_CPU"(
   START_TIME IN date,EQUIP_NAME in varchar2
)
as
   cpuused_now NUMBER;
   cpuused_last NUMBER;
   CPUUSED NUMBER;
BEGIN
   select CPUUSED into cpuused_now from S_PM_EQUIP_CPU where STARTTIME=START_TIME and EQUIPNAME=EQUIP_NAME;
   select CPUUSED into cpuused_last from S_PM_EQUIP_CPU where STARTTIME=START_TIME-30/1440 and EQUIPNAME=EQUIP_NAME;
   CPUUSED:=abs(cpuused_now-cpuused_last)/cpuused_last*100;
   insert into ALARM_S_PM_EQUIP_CPU VALUES (START_TIME,START_TIME-30/1440,EQUIP_NAME,CPUUSED) ;
   commit;
END PRO_S_PM_EQUIP_CPU;
入库模块:
Tryinsert:
$sth_insert_newobj_to_change->execute||((my $r=$DBI::err)&&(my $str=$DBI::errstr));
if($r)
{

#该数据表中可能已经存在
if( $r!=137) {
my $logmess = "(Message:13060) The data already in DB and will be updated !";
&writelog ( \$logmess, 1, 2 );
#update_pmdata($sub_sql_cols,$sub_sql_value,$sub_sql_where,$sub_tablename,$sub_dbh);
$r=update_pmdata($sub_sql_cols,$sql_cols,$sql_value,$primary_cols,$primary_values,$sub_tablename,$sub_dbh); #锁表情况
} elsif ( $r==137) {
$sub_locknum++;
#insert操作时,出现严重锁表情况,程序退出
if ( $sub_locknum > $sub_lockmax ) {
my $logmess = "(Error:13068) Critical error in insert the $sub_tablename, the table is locked more than $sub_locknum times !";
&writelog ( \$logmess, 1, 2 );
&exitload;
}
my $logmess = "(Error:13069) Error in insert the $sub_tablename, the table is locked, try again !";
&writelog ( \$logmess, 3, 2 );
$r=0;
goto Tryinsert;

解决方案 »

  1.   

    "触发器以后数据不是insert而是update导致数据入库失败"
    那错误是发生在手动更新表数据的时候吗?
    错误信息是什么?
      

  2.   

    >[错误] 脚本行:6-6 ---------------------------------------
     ORA-04092: cannot COMMIT in a trigger
    ORA-06512: at "STWG.PRO_S_PM_EQUIP_CPU", line 13
    ORA-06512: at "STWG.TRG_S_PM_EQUIP_CPU", line 2
    ORA-04088: error during execution of trigger 'STWG.TRG_S_PM_EQUIP_CPU'
     脚本行 6,语句行 1,列 12 
      

  3.   

    触发器中不能出现commit语句。