当前位置: 代码迷 >> Sql Server >> sql储存过程循环有关问题
  详细解决方案

sql储存过程循环有关问题

热度:82   发布时间:2016-04-27 13:45:15.0
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)asbegin  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_strendgo--查询各节点的父路径函数(从子到父)create function f_pid2(@id varchar(3)) returns varchar(100)asbegin  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_strendgoselect * ,        dbo.f_pid1(id) [路径(从父到子)] ,       dbo.f_pid2(id) [路径(从子到父)]from tb order by iddrop function f_pid1 , f_pid2drop 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 allselect 2,'广州',0 union allselect 3,'保定',1 union allselect 4,'同要',3 union allselect 5,'5街',4 union allselect 6,'海珠区',2 union allselect 7,'红海',6 union allselect 8,'中川路',7 union allselect 9,'红海江',6 union allselect 10,'中川工',6 union allselect 11,'中川',4declare @name varchar(4)set @name='广州';with tas(select * from tbl where Name='广州'union allselect tbl.* from t, tblwhere t.ID=tbl.Parentid)select * from t order by ID/*ID    Name    Parentid2    广州    06    海珠区    27    红海    68    中川路    79    红海江    610    中川工    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,Parentid) values(6,'海珠区',2)insert into tb_area(ID,Name,Parentid) values(7,'红海',6)insert into tb_area(ID,Name,Parentid) values(8,'中川路',7)insert into tb_area(ID,Name,Parentid) values(9,'红海江',6)insert into tb_area(ID,Name,Parentid) values(10,'中川工',6)insert into tb_area(ID,Name,Parentid) values(11,'中川',4)select * from tb_area---创建存储过程CREATE PROCEDURE up_find_area  @firstname varchar(20),@lastname varchar(20)asbegindeclare @name varchar(20),@Parentid intdeclare @level intdeclare @result varchar(100)----声明表变量declare  @result_t table(result varchar(100))----用游标取出需要处理的最低一层的数据declare cur_area cursor    for     select name,Parentid from tb_area where Name like [email protected]+'%'    open cur_area    fetch cur_area into @name,@Parentid    while @@fetch_status=0    begin         set @result = @name        set @level = 4        ----循环四次        while @level <> 0            begin                select @result = Name + @result,@Parentid = Parentid,@name = name                    from tb_area where ID = @Parentid                set @level = @level -1            end        ----如果循环计算出来的最上层和输入一致,[email protected]_t        if @name = @firstname        begin            insert into @result_t(result)            select @result        end                fetch cur_area into @name,@Parentid    end        close cur_area    deallocate cur_areaselect result from @result_tend----在程序里面调用存储过程获得数据exec up_find_area '广州','中川'
  相关解决方案