加入收藏
最新动态
最新动态
ORACLE

Oracle表触发器并写入序列值

来源:未知 作者:admin 人气: 时间:2014-02-07
Java代码  收藏代码
  1. --创建 序列 seq_1  
  2. drop sequence seq_1;  
  3. create sequence seq_1   
  4. minvalue 1   
  5. maxvalue 999999999999999999999999999   
  6. start with 100000000000000  
  7. increment by 1   
  8. nocache;  
  9.   
  10. drop table test_Depart;  
  11. create table test_Depart    (   
  12.      did  varchar2(32)    not null primary key,   
  13.      dname  varchar2(32)   not null   
  14. );   
  15.   
  16.   
  17. --建解发器代码为   
  18. create or replace trigger tri_test_id  
  19.   before insert on test_Depart   --test_Depart 是表名  
  20.   for each row  
  21. declare  
  22.   nextid varchar2(32);  
  23. begin  
  24.   IF :new.did IS NULL or :new.did=0 THEN --did是列名  
  25.      --取得年度  
  26.     select to_char(sysdate,'yyyy')||seq_1.nextval  
  27.     into nextid  
  28.     from sys.dual;  
  29.     :new.did:=nextid;  
  30.   end if;  
  31. end tri_test_id;  
  32.   
  33.   
  34. -- for test  
  35. --select to_char(sysdate,'yyyy')||seq_1.nextval from dual;  
  36.   
  37. --insert into test_Depart(dname) values('部门1');  



或者 
 

Java代码  收藏代码
  1. --建解发器代码为   
  2. create or replace trigger tri_test_id  
  3.   before insert on test_Depart   --test_Depart 是表名  
  4.   for each row  
  5. declare  
  6.   nextid number;  
  7. begin  
  8.   IF :new.did IS NULL or :new.did=0 THEN --did是列名  
  9.     select seq_1.nextval  
  10.     into nextid  
  11.     from sys.dual;  
  12.     :new.did:=nextid;  
  13.   end if;  
  14. end tri_test_id;  



或者 
 

Java代码  收藏代码
  1. --建解发器代码为   
  2. create or replace trigger tri_test_id  
  3.   before insert on test_Depart   --test_Depart 是表名  
  4.   for each row  
  5. declare  
  6.   nextid number;  
  7. begin  
  8.   IF :new.did IS NULL or :new.did=0 THEN --did是列名  
  9.     select to_number(to_char(sysdate,'yyyymmddHH24miss'))  
  10.     into nextid  
  11.     from sys.dual;  
  12.     :new.did:=nextid;  
  13.   end if;  
  14. end tri_test_id;  
上一篇:没有了
下一篇:Oracle 查询锁表和解锁
  • 用友
  • IBM
  • oracle
  • 联想
  • 华为
  • 思科
  • 公司地址:广州市天河区燕岭路95号四楼404室A20 邮编:510000
    电话:020-31600147 传真:020-31600147
      Copyright © 2015-2024 yunyuns.cn 云云(广州)科技有限公司 版权所有 ICP备13000495号-10