Oracle 中使用GUID

在Oracle 中虽然也有sys_guid() 函数可以生成GUID,但是与实际使用的格式有区别,为了满足需求,我们这里可以对函数进行封装处理

create or replace function getGUID return varchar2
as
  str varchar2(50);
  rt varchar2(50);
begin
  select sys_guid() into str from dual;
  select substr(str,1,8)||'-'||substr(str,9,4)||'-'||substr(str,13,4)||'-'||substr(str,17,4)||'-'||substr(str,21,12) into rt from dual;
  return rt;
end;

select getguid() from dual;  --测试
create table tmp01(id varchar2(50),name varchar2(30));  --建表
create trigger mytrigger  before insert on tmp01  --建触发器
for each row
begin
  select getguid() into :new.id from dual;
end;
insert into tmp01 values(null,'item1');
commit;
select * from tmp01;
  • 效果
posted @ 2023-03-17 16:36  丹心石  阅读(618)  评论(0)    收藏  举报