oracle觸發器產生單據號
CREATE OR REPLACE TRIGGER TRG_KC_SPRK_i
BEFORE INSERT ON KC_SPRK
FOR EACH ROW
DECLARE
vSCBH varchar2(40);
iCount NUMBER;
iFor number;
BEGIN
select t.bhqz into vSCBH from xt_djbhgz t where t.gzid=8;--這裡是獲取另一張表的某一個字段作為前綴,或者直接賦值也行: vSCBH := ‘GXCB_’
select count(1)+1 INTO iCount from KC_SPRK j where j.xzsj>=trunc(sysdate,'mm');
--這裡是獲取本張表的總數 + 1,並賦值給iCount
vSCBH:=vSCBH||to_char(sysdate,'yyyymm');
--這裡是前綴+時間年月
FOR iFor IN 1..5-LENGTH(iCount) LOOP
vSCBH:=vSCBH||'0';
END LOOP;
:new.RK_DJH:=vSCBH||iCount;
END TRG_KC_SPRK_i;
效果:GXCB_20141100001 GXCB_20141100002 GXCB_20141100003