--查询处理的函数
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
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
解决方案 »
- rowlock 在事务里有用吗?
- 组合式的更新问题
- 如何取出一个表中字段值相同的记录进行统计
- 给个简单的思路,如何计算啊??
- 我想弄清楚一下触发器!请各位能否解答我心中的疑问呢?谢谢!
- sql语言中有没有类似C语言中的switch case的语句??
- SQLServer怎样通过写语句来把一个文件直接写到类型为image的字段中来?
- 怎样用sql表达“15日以内的记录”
- 这样的功能可以做吗?(PB+SQL SERVER)
- 如何知道已经执行过的SQL语句
- delete from test where not in (select test from group by name) 看看这条语条为什么错误?
- 急求sql语句,查看所有表名
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
我把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
是通过了,但是什么也查不出来。
select f_cid(1) --看看是否查出了1的所有子类--2.你列的示例数据有些不对吧? id=1的,它应该不是最末级,islast怎么会是1呢?
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 行)--*/
------------------
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
------------------
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