查询中断的记录
2013-12-27 16:20 ycm 阅读(442) 评论(0) 编辑 收藏 举报/*
说明:#check_list_bak 的基本结构为
-----------------
card_sequ 为流水号 正常的情况自增
在实际中经常出现流水号断了的情况,为了便于找到断的流水号
需要查询 以下 是查询的办法
基本想法是 ,创建1个临时表 存储 card_sequ 及new_card_sequ (总比card_sequ大1) 然后比较
*/
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#check_list_bak') and type='U')
begin
drop table #check_list_bak
end
--new_card_sequ 为 card_sequ的下一条记录 得到方法如下
select card_sequ,new_card_sequ
into #check_list_bak
from (select card_sequ,new_card_sequ =
(select top 1 card_sequ
from #check_list
where card_sequ=(select min(card_sequ)
from #check_list
)
from #check_list as a) as b
--得到中断流水号上下条记录的 流水号
select * from #check_list_bak where card_sequ <> new_card_sequ - 1
select a.* from #check_list a inner join (
select *
from #check_list_bak
where card_sequ <> new_card_sequ - 1) b
on a.card_sequ = b.card_sequ or a.card_sequ=b.new_card_sequ
order by a.card_sequ where card_sequ>a.card_sequ)
order by card_sequ