/* 标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示) 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 */create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) insert into tb values('001' , null , '广东省') insert into tb values('002' , '001' , '广州市') insert into tb values('003' , '001' , '深圳市') insert into tb values('004' , '002' , '天河区') insert into tb values('005' , '003' , '罗湖区') insert into tb values('006' , '003' , '福田区') insert into tb values('007' , '003' , '宝安区') insert into tb values('008' , '007' , '西乡镇') insert into tb values('009' , '007' , '龙华镇') insert into tb values('010' , '007' , '松岗镇') go--查询指定节点及其所有子节点的函数 create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) as begin declare @level int set @level = 1 insert into @t_level select @id , @level while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.id , @level from tb a , @t_Level b where a.pid = b.id and b.level = @level - 1 end return end go--调用函数查询001(广东省)及其所有子节点 select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 002 001 广州市 003 001 深圳市 004 002 天河区 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇(所影响的行数为 10 行) */
with cte as ( select * from 地区表 where areaid=1 union all select a.* from 地区表 a,cte b where a.parentid=b.areaid )select * from cte
with cte as ( select * from 地区表 where areaid=1 union all select a.* from 地区表 a,cte b where a.parentid=b.areaid )select * from 公司详细信息表 where areaid in (select areaid from cte) 另外,你用的是sql什么版本
sql server 2000 cte这是什么东西? 存储过程?
--查询指定节点及其所有子节点的函数 create function f_cid(@ID int) returns @t_level table(areaid int , level int) as begin declare @level int set @level = 1 insert into @t_level select @id , @level while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.areaid , @level from 地区表 a , @t_Level b where a.parentid = b.areaid and b.level = @level - 1 end return endgoselect * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1))
前面用的是sql 2005的语法,sql 2000中没有的,上面换了一个使用自定义函数的方法
select * from 公司详细信息表 where areaid in ( select a.areaid from 地区表 a, 地区表 b where a.parentid=b.areaid and b.area='安徽省' union all select a.areaid from 地区表 a, 地区表 b,地区表 c where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省' )
select * from 公司详细信息表 where areaid in ( select a.areaid from 地区表 a, 地区表 b where a.parentid=b.areaid and b.area='安徽省' union all select a.areaid from 地区表 a, 地区表 b,地区表 c where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省' )
SQL code--查询指定节点及其所有子节点的函数 create function f_cid(@ID int) returns @t_level table(areaid int , level int) as begin declare @level int set @level = 1 insert into @t_level select @id , @level while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.areaid , @level from 地区表 a , @t_Level b where a.parentid = b.areaid and b.level = @level - 1 end return endgoselect * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1)) 这方法我用了 很好很强大 是我要的效果。 不过怎么给分啊 。
create table bookArea(area_id int,book_parentid int,book_area nvarchar(50)) insert bookArea select 1,0,N'所有地区' union all select 2,1,N'安徽省' union all select 3,2,N'合肥市' union all select 4,3,N'长丰县' union all select 5,3,N'肥东县' union all select 6,3,N'肥西县' gocreate function f_cid(@ID int) returns @t_level table(area_id int,book_area nvarchar(50) , level int) as begin declare @level int declare @area nvarchar(50) set @level = 1 select @area=book_area from bookArea where area_id=@ID --加上这句赋值 insert into @t_level select @id,@area,@level --原来@area未赋值,所以是null while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.area_id ,a.book_area, @level from bookArea a , @t_Level b where a.book_parentid = b.area_id and b.level = @level - 1 end return endgo select * from f_cid(2)/* area_id book_area level ----------- -------------------------------------------------- ----------- 2 安徽省 1 3 合肥市 2 4 长丰县 3 5 肥东县 3 6 肥西县 3(5 行受影响) */
地区表:areaid area parentid
1 所有地区 0
2 安徽省 1
3 合肥市 2
4 长丰县 3
5 肥东县 3
6 肥西县 3
这该怎么查出所有安徽省的公司。?
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 10 行)
*/
(
select * from 地区表 where areaid=1
union all
select a.* from 地区表 a,cte b where a.parentid=b.areaid
)select * from cte
(
select * from 地区表 where areaid=1
union all
select a.* from 地区表 a,cte b where a.parentid=b.areaid
)select * from 公司详细信息表 where areaid in (select areaid from cte) 另外,你用的是sql什么版本
create function f_cid(@ID int) returns @t_level table(areaid int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.areaid , @level
from 地区表 a , @t_Level b
where a.parentid = b.areaid and b.level = @level - 1
end
return
endgoselect * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1))
where areaid
in (
select a.areaid from 地区表 a, 地区表 b
where a.parentid=b.areaid and b.area='安徽省'
union all
select a.areaid from 地区表 a, 地区表 b,地区表 c
where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省'
)
where areaid
in (
select a.areaid from 地区表 a, 地区表 b
where a.parentid=b.areaid and b.area='安徽省'
union all
select a.areaid from 地区表 a, 地区表 b,地区表 c
where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省'
)
create function f_cid(@ID int) returns @t_level table(areaid int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.areaid , @level
from 地区表 a , @t_Level b
where a.parentid = b.areaid and b.level = @level - 1
end
return
endgoselect * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1)) 这方法我用了 很好很强大 是我要的效果。 不过怎么给分啊 。
insert bookArea
select 1,0,N'所有地区' union all
select 2,1,N'安徽省' union all
select 3,2,N'合肥市' union all
select 4,3,N'长丰县' union all
select 5,3,N'肥东县' union all
select 6,3,N'肥西县'
gocreate function f_cid(@ID int) returns @t_level table(area_id int,book_area nvarchar(50)
, level int)
as
begin
declare @level int
declare @area nvarchar(50)
set @level = 1
select @area=book_area from bookArea where area_id=@ID --加上这句赋值
insert into @t_level select @id,@area,@level --原来@area未赋值,所以是null
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.area_id ,a.book_area, @level
from bookArea a , @t_Level b
where a.book_parentid = b.area_id and b.level = @level - 1
end
return
endgo
select * from f_cid(2)/*
area_id book_area level
----------- -------------------------------------------------- -----------
2 安徽省 1
3 合肥市 2
4 长丰县 3
5 肥东县 3
6 肥西县 3(5 行受影响)
*/