一张表T_JZG_JBXX
主要字段为:
ZGH   职工号       NOT MULL
XM    中文姓名     NOT MULL
XMPY  姓名拼音     NULL
客户想实现姓名首字母查询,现已对表中的XMPY进行了维护,但是对今后再增加的人员也要XMPY自动转换为中文姓名的首字母。
本人做了个触发器:create or replace trigger biu_t_jzg_jbxx_XMPY
  after insert on t_jzg_jbxx
  referencing old as old_value 
              new as new_value
  for each rowbegin
  if inserting then
    update t_jzg_jbxx
       set xmpy = F_TRANS_PINYIN_CAPITAL(:new_value.xm)
     where zgh = :new_value.zgh;
  end if;
end;
编译通过,而且在测试表中也能够实现自动更新姓名首字母,但是在实际的系统中,添加人员时,系统会报错。
公司同事认为触发器不稳定,让我做个脚本 服务器定时执行函数
但我对脚本一点都不熟悉。希望大家告诉我这个脚本怎么写。
谢谢了!

解决方案 »

  1.   


    --给你一个JOB参考下..
    --job_action 是你要定时跑的 过程.. 我的例子 每天10点跑。
    begin
      sys.dbms_scheduler.create_job(job_name            => '任务名称',
                                    job_type            => 'STORED_PROCEDURE',
                                    job_action          => 'scott.Pkg_业务处理.Prc_更新姓名拼音',
                                    start_date          => to_date('21-09-2012 00:00:00', 'dd-mm-yyyy hh24:mi:ss'),
                                    repeat_interval     => 'Freq=Daily;Interval=1;ByHour=22;ByMinute=00;BySecond=00',
                                    end_date            => to_date(null),
                                    job_class           => 'DEFAULT_JOB_CLASS',
                                    enabled             => true,
                                    auto_drop           => false,
                                    comments            => '更新姓名拼音');
    end;
    /
      

  2.   

    你要想图形化建的话,PL/SQL 左边列表有个DBMS_JOBS(老版本) 或者 JOBS(新版本),直接右键新建也行。
      

  3.   

    create or replace trigger biu_t_jzg_jbxx_XMPY
      after insert or update on t_jzg_jbxx
      for each row
    begin
      if :new.xm is not null then
       set :new.xmpy = F_TRANS_PINYIN_CAPITAL(:new.xm)
      end if;
    end;-- 还有:你的 F_TRANS_PINYIN_CAPITAL 函数应该有异常处理部分吧?
    -- 例如:如果要转换的参数中不含中文字符等这样的情况,执行这个函数会不会报错?
      

  4.   

    如果你在触发器中,所触发的动作是涉及源触发表的DML操作,则需要用自治事务!
      

  5.   

    触发器里不要update,直接 :new_value.xm = F_TRANS_PINYIN_CAPITAL(:new_value.xm)
      

  6.   

    -- *1. 重新编写你的触发器,代码类似如下:
    create or replace trigger biu_t_jzg_jbxx_XMPY
      after insert or update on t_jzg_jbxx
      for each row
    begin
      if :new.xm is not null then
        :new.xmpy = F_TRANS_PINYIN_CAPITAL(:new.xm)
      end if;
    end;
    -- *2. 确保 F_TRANS_PINYIN_CAPITAL 有异常处理部分,代码类似如下:CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(par1 data_type, ..., parn data_type)
    IS
    BEGIN...EXCEPTION WHEN OTHERS THEN
      RETURN NULL;
    END;END;
    /