发票库:
起始编号 终止编号
 005 010
 020 030
领取发票,起始编号008 终止编号025
希望系统提示发票不存在。
请教此查询的写法。

解决方案 »

  1.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(起始编号 varchar(8), 终止编号 varchar(8))
    insert into #
    select '005', '010' union all
    select '020', '030'if not exists (select 1 from # where 起始编号<='008' and 终止编号>='025')
    select '提示发票不存在'
      

  2.   

    有连续一样的思维
    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(起始编号 varchar(8), 终止编号 varchar(8))
    insert into #
    select '005', '006' union all
    select '007', '010' union all
    select '020', '030'if not exists (select 1 from # a left join # b on a.起始编号 = b.终止编号 + 1
    where isnull(b.起始编号,a.起始编号)<='008' and isnull(a.终止编号, b.终止编号) >= '010')
    select '提示发票不存在'else
    select '提示发票存在'