--查询处理的函数
create function f_cid(@id int)
returns @re table(id int,level int,islast bit)
as
begin
declare @l int
set @l=0
insert @re select id,@l,islast from class where id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,a.islast
from class a,@re b
where a.parent_id=b.id and b.level=@l-1
end
delete from @re where islast=1
end
go--调用函数实现查询(查询基础1)
select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid

解决方案 »

  1.   

    --查询处理的函数
    create function f_cid(@id int)
    returns @re table(id int,level int,islast bit)
    as
    begin
    declare @l int
    set @l=0
    insert @re select id,@l,islast from class where id=@id
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.id,@l,a.islast
    from class a,@re b
    where a.parent_id=b.id and b.level=@l-1
    end
    delete from @re where islast=1
    return  --少了
    end
    go--调用函数实现查询(查询基础1)
    select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid
      

  2.   

    还是出不来。
    我把select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid
    改成
    select top 5 a.* from info a,f_cid(1) b where a.classid=b.id
    是通过了,但是什么也查不出来。
      

  3.   

    --1.
    select f_cid(1) --看看是否查出了1的所有子类--2.你列的示例数据有些不对吧? id=1的,它应该不是最末级,islast怎么会是1呢?
      

  4.   

    create table class(id int,parent_id int,classname varchar(10),islast bit)
    insert class select 1,0,'类一',1
    union all select 2,1,'类二',0
    union all select 3,1,'类三',1
    union all select 4,2,'类四',0
    union all select 5,4,'类五',1create table info(id int,classid int,name varchar(10))
    insert info select 1,1,'X'
    union all select 2,2,'T'
    union all select 3,1,'Y'
    union all select 4,2,'Z'
    union all select 5,3,'C'
    union all select 6,1,'T'
    go--查询处理的函数
    create function f_cid(@id int)
    returns @re table(id int,level int,islast bit)
    as
    begin
    declare @l int
    set @l=0
    insert @re select id,@l,islast from class where id=@id
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.id,@l,a.islast
    from class a,@re b
    where a.parent_id=b.id and b.level=@l-1
    end
    delete from @re where islast=0
    return
    end
    go--调用函数实现查询(查询基础1)
    select top 5 a.* from info a,f_cid(1) b where a.classid=b.id
    go--删除测试
    drop table class,info
    drop function f_cid/*--结果id          classid     name       
    ----------- ----------- ---------- 
    1           1           X
    3           1           Y
    5           3           C
    6           1           T(所影响的行数为 4 行)--*/
      

  5.   

    class表
    ------------------
    id parent_id classname islast
    1    0        类一      0
    2    1        类二      0
    3    1        类三      1
    4    2        类四      0
    5    4        类五      1
    5    1        类六      1info表
    ------------------
    id classid name
    1    1      X
    2    2      T
    3    1      Y
    4    2      Z
    5    3      C
    6    1      T我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于类一下的哪个字类的的classid,
    也就是这样的,那应该怎么修改?
    id          classid     name       
    ----------- ----------- ---------- ---------- 
    1           1           X             
    3           1           Y
    5           3           C
    6           1           T
      

  6.   

    修改了一下,class表
    ------------------
    id parent_id classname islast
    1    0        类一      0
    2    1        类二      0
    3    1        类三      1
    4    2        类四      0
    5    4        类五      1
    6    1        类六      1info表
    ------------------
    id classid name
    1    3      X
    2    5      T
    3    4      Y
    4    6      Z
    5    3      C
    6    5      T我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于"类一"下的哪个子类的的classid,
    也就是这样的
    id          classid     name         classid2   
    ----------- ----------- ---------- ---------- 
    1           3           X                3                      
    3           4           Y                2
    5           3           C                3
    6           5           T                2