select istart, iend, iend - istart + 1 as imiss from
(
select ibillid + 1 as istart, (select min(ibillid)
from stCardBill as b
where b.ibillid > a.ibillid
) - 1 as iend
from stCardBill as a
where not exists ( select *
from stCardBill as b
where b.ibillid = a.ibillid + 1 ) and
a.ibillid < (select max(ibillid)
from stCardBill
)
) as w
order by istart
虽然重用被删除键值不是个好主意,但至少可以告诉客户或自己清楚那些编号缺失了。