参考zjcxc 的帖子使用函数比较好
create function f_qryLever(@id int)
returns @r table(DataID int,level int)
as 
begin
declare @l int
set @l=0
insert into @r select ParentID ,@l From T_Data Where DataID = @id
while @@rowcount>0
begin
set @l=@l+1
insert into @r select T_Data.DataID,@l
from T_Data join @r b on T_Data.ParentID=b.DataID
where b.level=@l-1
end
return
end
查询语句:
select T_Data.* from T_Data
where DataID In(select DataID from f_qryLever(361) Where level = 1)

解决方案 »

  1.   

    --创建处理函数
    create function f_qry()
    returns @re table(DataName varchar(8000))
    as
    begin
    declare @t table(id int,level int,DataName varchar(8000))
    declare @l int
    set @l=0
    insert @t select DataID,@l,DataName
    from T_Data
    where ParentID=0
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @t select a.DataID,@l,b.DataName+' '+a.DataName
    from T_Data a join @t b on a.ParentID=b.id and b.level=@l-1
    end
    insert @re select DataName
    from @t a
    where not exists(
    select 1 from T_Data
    where ParentID=a.id)
    order by id
    return
    end
    go--调用实现查询
    select * from f_qry()
      

  2.   

    --测试--测试数据
    CREATE TABLE [dbo].[T_Data] (
    [DataID] [int] IDENTITY (1, 1) NOT NULL ,
    [DataName] [varchar] (128) COLLATE Chinese_PRC_CI_AS NULL ,
    [ParentID] [int] NULL 
    ) ON [PRIMARY]
    insert T_Data select '材料'    ,0
    union  all    select '办公用品',1
    union  all    select '纸制品'  ,2
    union  all    select '文具'    ,2
    union  all    select '热水壶'  ,1
    GO--创建处理函数
    create function f_qry()
    returns @re table(DataName varchar(8000))
    as
    begin
    declare @t table(id int,level int,DataName varchar(8000))
    declare @l int
    set @l=0
    insert @t select DataID,@l,DataName
    from T_Data
    where ParentID=0
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @t select a.DataID,@l,b.DataName+' '+a.DataName
    from T_Data a join @t b on a.ParentID=b.id and b.level=@l-1
    end
    insert @re select DataName
    from @t a
    where not exists(
    select 1 from T_Data
    where ParentID=a.id)
    order by id
    return
    end
    go--调用实现查询
    select * from f_qry()
    go--删除测试
    drop table T_Data
    drop function f_qry/*--测试结果DataName                
    ------------------------
    材料 办公用品 纸制品
    材料 办公用品 文具
    材料 热水壶(所影响的行数为 3 行)
    --*/
      

  3.   

    谢谢各位刚才使用了 cnwolfs(cnwolfs) 写的请问select T_Data.* from T_Data
    where DataID In(select DataID from f_qryLever(361) Where level = 1)
    361这个参数如何动态、批量指定?也就是指定成T_Data.DataID现在我再参考一下zjcxc(邹建) 的帖子
      

  4.   

    to zjcxc(邹建)--创建处理函数
    create function f_qry()
    returns @re table(DataName varchar(8000))
    as
    begin
    declare @t table(id int,level int,DataName varchar(8000))
    declare @l int
    set @l=0
    insert @t select DataID,@l,DataName
    from T_Data
    where ParentID=0
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @t select a.DataID,@l,b.DataName+' '+a.DataName
    from T_Data a join @t b on a.ParentID=b.id and b.level=@l-1
    end
    insert @re select DataName            -- 此处起什么作用?在我的2158条数据之中,花费了9秒的时间,有点慢,去除这条Insert语句,只花费了0.110秒
    from @t a
    where not exists(
    select 1 from T_Data
    where ParentID=a.id)
    order by id
    return
    end
    go