参考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)
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)
解决方案 »
- 分组求聚合
- 两个数据库查询的小题
- SQL Server性能查看的一些问题
- 水晶报表登录sql失败,提示null用户
- 1个简单的数据库问题,求大侠解答
- 请高手指点有什么办法可以实现?
- 数据库面试题。。。(急)--求sql语句。
- 跟一家20多个人的美容美体店做一个顾客管理程序,提供所有顾客的消费情况记录、指定顾客的消费记录查询和月收入统计等功能,开价多少?
- 我想问问TNS(在Oracle中接触到的)是什么的缩写呀,是什么网络协议呀?请说的详细一点,谢谢
- 可不可以写在Insert前发生的触发器
- 在MS SQL Server 2000中,是否可以并发执行同一存储过程!?!?!?!?
- 请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
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()
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 行)
--*/
where DataID In(select DataID from f_qryLever(361) Where level = 1)
361这个参数如何动态、批量指定?也就是指定成T_Data.DataID现在我再参考一下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