如题:示例:原列列1
2
3
4
8
1
结果:列1
1
2
3
4
8输出:5,6,7不存在这个sql怎么写?

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-05-28 09:44:17
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([列1] int)
    insert [tb]
    select 2 union all
    select 3 union all
    select 4 union all
    select 8 union all
    select 1
    --------------开始查询--------------------------select number from master..spt_values where number between 1 and 8 and type='p' and number not in(select [列1] from tb)
    ----------------结果----------------------------
    /* number
    -----------
    5
    6
    7(3 行受影响)
    */
      

  2.   

    select id+1,(select min(id)-1 from t c where c.id>id+1) from t a
    where not exists (select * from t b 
    where b.id=t.id+1) ;
    输出是个断号的范围
      

  3.   

    改下
    select id+1,(select min(id)-1 from t c where c.id>id+1) from t a
    where not exists (select * from t b 
    where b.id=a.id+1) ;
      

  4.   

    路过,学习.... master..spt_values 判断断号 0~2047 (SQL 2K5)