--直接补,这样的方式,Fid的值最大从0~10000
select Fid=b.id,Fname=isnull(Fname,'')
from 表 a right join(
select id=a.id+b.id+c.id+d.id+1
from(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) a,(
select id=0 union all select 10
union all select id=20 union all select 30
union all select id=40 union all select 50
union all select id=60 union all select 70
union all select id=80 union all select 90
) b,(
select id=0 union all select 100
union all select id=200 union all select 300
union all select id=400 union all select 500
union all select id=600 union all select 700
union all select id=800 union all select 900
) c,(
select id=0 union all select 1000
union all select id=2000 union all select 3000
union all select id=4000 union all select 5000
union all select id=6000 union all select 7000
union all select id=8000 union all select 9000
) d
)b on a.fid=b.id
where b.id<=(select max(fid) from 表)
order by b.id

解决方案 »

  1.   

    --测试--测试数据
    create table 表(Fid int,Fname varchar(10))
    insert 表 select 1,'A'
    union all select 2,'B'
    union all select 3,'C'
    union all select 6,'F'
    go--查询
    select Fid=b.id,Fname=isnull(Fname,'')
    from 表 a right join(
    select id=a.id+b.id+c.id+d.id+1
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b,(
    select id=0 union all select 100
    union all select id=200 union all select 300
    union all select id=400 union all select 500
    union all select id=600 union all select 700
    union all select id=800 union all select 900
    ) c,(
    select id=0 union all select 1000
    union all select id=2000 union all select 3000
    union all select id=4000 union all select 5000
    union all select id=6000 union all select 7000
    union all select id=8000 union all select 9000
    ) d
    )b on a.fid=b.id
    where b.id<=(select max(fid) from 表)
    order by b.id
    go--删除测试
    drop table 表/*--测试结果
    Fid         Fname      
    ----------- ---------- 
    1           A
    2           B
    3           C
    4           
    5           
    6           F(所影响的行数为 6 行)
    --*/
      

  2.   

    --用临时表的方法,Fid的值不受限制
    declare @i int
    select @i=max(Fid) from 表
    set rowcount @i
    select id=identity(int,1,1),a=0 into #t from syscolumns
    set @i=@i-@@rowcount
    while @i>0
    begin
    set rowcount @i
    insert #t select 0 from syscolumns
    set @i=@i-@@rowcount
    endselect Fid=b.id,Fname=isnull(Fname,'')
    from 表 a right join #t b on a.fid=b.id
    where b.id<=(select max(fid) from 表)
    order by b.id
      

  3.   

    --测试--测试数据
    create table 表(Fid int,Fname varchar(10))
    insert 表 select 1,'A'
    union all select 2,'B'
    union all select 3,'C'
    union all select 6,'F'
    go--用临时表的方法,Fid的值不受限制
    declare @i int
    select @i=max(Fid) from 表
    set rowcount @i
    select id=identity(int,1,1),a=0 into #t from syscolumns
    set @i=@i-@@rowcount
    while @i>0
    begin
    set rowcount @i
    insert #t select 0 from syscolumns
    set @i=@i-@@rowcount
    endselect Fid=b.id,Fname=isnull(Fname,'')
    from 表 a right join #t b on a.fid=b.id
    where b.id<=(select max(fid) from 表)
    order by b.id
    go--删除测试
    drop table 表/*--测试结果
    Fid         Fname      
    ----------- ---------- 
    1           A
    2           B
    3           C
    4           
    5           
    6           F(所影响的行数为 6 行)
    --*/