日期:2014-05-18  浏览次数:20742 次

sql储存过程循环问题
表table里面内容
ID Name Parentid
1 北京 0
2 广州 0
3 保定 1
4 同要 3
5 5街 4
6 海珠区 2
7 红海 6
8 中川路 7
9 红海江 6
10 中川工 6
11 中川 4

现在想通过模糊查询找到下面数据.例如我输入“中川” 选择广州
显示结果为:

广州海珠区红海中川路
广州海珠区红海中川工
在存储过程里面可如何用游标实现呀?

------解决方案--------------------
SQL code

/*
标题:SQL SERVER 2000中查询指定节点及其所有父节点的函数(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
时间:2010-02-02
地点:新疆乌鲁木齐
*/

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_pid1(@id varchar(3)) returns varchar(100)
as
begin
  declare @re_str as varchar(100)
  set @re_str = ''
  select @re_str = name from tb where id = @id
  while exists (select 1 from tb where id = @id and pid is not null)
    begin
      select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
    end
  return @re_str
end
go
--查询各节点的父路径函数(从子到父)
create function f_pid2(@id varchar(3)) returns varchar(100)
as
begin
  declare @re_str as varchar(100)
  set @re_str = ''
  select @re_str = name from tb where id = @id
  while exists (select 1 from tb where id = @id and pid is not null)
    begin
      select @id = b.id , @re_str = @re_str + ',' + b.name from tb a , tb b where a.id = @id and a.pid = b.id
    end
  return @re_str
end
go

select * , 
       dbo.f_pid1(id) [路径(从父到子)] ,
       dbo.f_pid2(id) [路径(从子到父)]
from tb order by id

drop function f_pid1 , f_pid2
drop table tb

/*
id   pid  name    路径(从父到子)               路径(从子到父)              
---- ---- ------  ---------------------------  ----------------------------
001  NULL 广东省  广东省                       广东省
002  001  广州市  广东省,广州市                广州市,广东省
003  001  深圳市  广东省,深圳市                深圳市,广东省
004  002  天河区  广东省,广州市,天河区         天河区,广州市,广东省
005  003  罗湖区  广东省,深圳市,罗湖区         罗湖区,深圳市,广东省
006  003  福田区  广东省,深圳市,福田区         福田区,深圳市,广东省
007  003  宝安区  广东省,深圳市,宝安区         宝安区,深圳市,广东省
008  007  西乡镇  广东省,深圳市,宝安区,西乡镇  西乡镇,宝安区,深圳市,广东省
009  007  龙华镇  广东省,深圳市,宝安区,龙华镇  龙华镇,宝安区,深圳市,广东省
010  007  松岗镇  广东省,深圳市,宝安区,松岗镇  松岗镇,宝安区,深圳市,广东省

(所影响的行数为 10 行)
*/

------解决方案--------------------
SQL code

--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([ID] int,[Name] varchar(6),[Parentid] int)
insert [tbl]
select 1,'北京',0 union all
select 2,'广州',0 union all
select 3,'保定',1 union all
select 4,'同要',3 union all
select 5,'5街',4 union all
select 6,'海珠区',2 union all
select 7,'红海',6 union all
select 8,'中川路',7 union all
select 9,'红海江',6 union all
select 10,'中川工',6 union all
select 11,'中川',4
declare @name varchar(4)
set @name='广州'
;with t
as(
select * from tbl where Name='广州'
union all
select tbl.* from t, tbl
where t.ID=tbl.Parentid
)
select * from t order by ID

/*
ID    Name    Parentid
2    广州    0
6    海珠区    2
7    红海    6
8    中川路    7
9    红海江    6
10    中川工    6
*/
--到这一步了,不明白比还要怎么处理,怎么选择

------解决方案--------------------
SQL code

---建立实体表
create table tb_area
(ID int,
Name varchar(20),
Parentid int)

insert into tb_area(ID,Name,Parentid) values(1,'北京',0)
insert into tb_area(ID,Name,Parentid) values(2,'广州',0)
insert into tb_area(ID,Name,Parentid) values(3,'保定',1)
insert into tb_area(ID,Name,Parentid) values(4,'同要',3)
insert into tb_area(ID,Name,Parentid) values(5,'5街',4)
insert into tb_area(ID,Name,Paren