一个储存过程的问题?

Sylix 2003-10-20 01:46:44
在aa表中一字段数据只能在1-99999之间,现在已有从00001到99999之间的数据,但中间有许多不连序,现在要让之从小连序生成,
这个储存过程应该怎么写?
...全文
34 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sylix 2003-10-21
  • 打赏
  • 举报
回复
多谢各位,
to daydayupliq(强强),
以前是用序列,用触发器应该怎么做?


Lastdrop 2003-10-21
  • 打赏
  • 举报
回复
若是要最小的,可以
select min(new_value) from
( select col+1 new_value from aa a where not exists ( select 1 from aa b where b.col=(a.col+1)) );

若是任一个不连续的值,可以
select col+1 new_value from aa a where not exists ( select 1 from aa b where b.col=(a.col+1)) and rownum < 2;

后者的效率要高很多。
Lastdrop 2003-10-21
  • 打赏
  • 举报
回复
新增的时候,在目前的aa表中字段col的值中选取一个未用的
select col+1 from aa a where not exists ( select 1 from aa b where b.col=(a.col+1));
bzszp 2003-10-21
  • 打赏
  • 举报
回复
新增数据?
不明白你的意思
beckhambobo 2003-10-21
  • 打赏
  • 举报
回复
create trigger a_tri
before insert or update or delete on a
for each row
declare
v_id varchar2(20);
begin
select min(id) into v_id from a where (lead(id,1,0) over(order by rownum)-id)>1;
if inserting then
select v_id into :new.id from dual;
end if;
end;
/
daydayupliq 2003-10-20
  • 打赏
  • 举报
回复
这个问题值得关注!
得用个触发器吧!
能不能做掉重新开始呢?
那样用序列不就得了!
Sylix 2003-10-20
  • 打赏
  • 举报
回复
thanks,
to bzszp(SongZip) ,
是新增数据,不是修改数据.
bzszp 2003-10-20
  • 打赏
  • 举报
回复
不需要存储过程
update aa set col=(select tt.id from (select rownum id,t.* from (select * from aa order by col) t) tt where tt.col=aa.col);

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧