查询中断的记录

本文介绍了一种通过创建临时表来检测SQL数据库中流水号断号的方法。该方法适用于当流水号出现不连续情况时,能够快速定位到断号的具体位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
说明:#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

转载于:https://www.cnblogs.com/yucm/p/3494385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值