有这样一个表:(说明:ABCDEF列全是数值,NAME列可以为NULL)
DIZHI_ID name   A   B   C   D   E   F
1        jia    2   2   1   6   2   4
2         yi    1   0   5   2   4   3
3        bing   0   4   1   2   0   4
4        ding   0   4   4   7   4   2
5        NULL   4   4   3   2   1   1
6        mao    0   1   3   2   5   4
要实现这样一个功能:首先:检索yi   读出yi这行的所有的信息:
                     DIZHI_ID    NAME    A  B  C  D   E  F
                      2           YI     1  2  5  2   4  3
                    第二步:让A这列的数值(既:1)加上一个值
                            假设加1(即此时数值大小为2) 
                            此时检索A这列的值为2的NAME,同时
                            要满足如果A值为2的行NAME为NULL
                            则不输出此行!
                    第三步:和第2步类似:既YI这行的B列数值为0
                            让这个值加上一个数值……(和第2步的                            操作一样
                    第四步:读C的值……
                    第五步:读D的值……
                    (以此类推…)
小辈在这里有礼了,想请教各位前辈怎么用SQL语句完成?
先谢过前辈们……

解决方案 »

  1.   


    create table #t(DIZHI_ID int, name varchar(10),  A int,  B int, C int, D int, E int, F int)
    insert into #t
    select 1,'jia',2,2,1,6,2,4 union all
    select 2,'yi',1,0,5,2,4,3 union all
    select 3,'bing',0,4,1,2,0,4 union all
    select 4,'ding',0,4,4,7,4,2 union all
    select 5,'NULL',4,4,3,2,1,1 union all
    select 6,'mao',0,1,3,2,5,4declare @i int ,@a int,@b int, @c int,@d int, @e int,@f intset @i=1select @a=a,@b=b,@c=c,@d=d,@e=e,@f=f from #t where name='yi'set @a=@a+@iif exists (select * from #t where a=@a and [name] is not null)
          select * from #t where a=@aset @b=@b+@iif exists (select * from #t where b=@b and [name] is not null)
          select * from #t where b=@b
    set @c=@c+@iif exists (select * from #t where c=@c and [name] is not null)
          select * from #t where c=@cset @d=@d+@iif exists (select * from #t where d=@d and [name] is not null)
          select * from #t where d=@dset @e=@e+@iif exists (select * from #t where e=@e and [name] is not null)
          select * from #t where e=@e
    set @f=@f+@iif exists (select * from #t where f=@f and [name] is not null)
          select * from #t where f=@f
    drop table #t