流水号查询?

fxwlei3245 2008-03-11 10:26:29
本人在日常的查帐过程中,遇到一个问题,就是流水号都是系统按顺序自动生成的,但有时候不知道什么原因,中间却出现断号了,那么有没有什么SQL语句直接查到是断了那些号呢?
谢谢,
认识更多的朋友,学习更多的知识
...全文
1417 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
niming520 2008-03-12
  • 打赏
  • 举报
回复
create table #t1
(
id int IDENTITY(1,1),
job int
)
create table #t2
(
id int IDENTITY(1,1),
job int
)
declare @a int,@b int,@c int
set @a=1

insert into #t2 select job_id from jobs
set @b=(select max(job) from #t2)
select * from #t2
select @a,@b
while(@a<=@b)
begin
if ((select job from #t2 where job=@a)-(select job from #t2 where job=@a+1)=1)
continue

else
set @c=(select job from #t2 where job=@a)
insert into #t1 (job) values (@c)
if(@a=@b)
break

select @a=@a+1

end

select * from #t1 where job is null
drop table #t1
drop table #t2
dawugui 2008-03-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 fxwlei3245 的帖子:]
本人在日常的查帐过程中,遇到一个问题,就是流水号都是系统按顺序自动生成的,但有时候不知道什么原因,中间却出现断号了,那么有没有什么SQL语句直接查到是断了那些号呢?
谢谢,
认识更多的朋友,学习更多的知识
[/Quote]

如果要查所有的断号,必须先从最小到最大生成所有的号码,然后使用 not in 查询所有的号.
dawugui 2008-03-11
  • 打赏
  • 举报
回复

断号的开始号
set nocount on
declare @t table(a int,b char(6),flag int)
insert @t select 1,'one',NULL
insert @t select 2,'two',NULL
insert @t select 3,'three',NULL
insert @t select 5,'four',NULL
insert @t select 6,'five',NULL
insert @t select 10,'six',NULL
insert @t select 11,'seven',NULL

select *
from @t aa
where (a - (select max(a) from @t where aa.a > a)) > 1
/*

a b flag
----------- ------ -----------
5 four NULL
10 six NULL
*/

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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