表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现在想通过模糊查询找到下面数据.例如我输入“中川” 选择广州
显示结果为:广州海珠区红海中川路
广州海珠区红海中川工
在存储过程里面可如何用游标实现呀?

解决方案 »

  1.   


    /*
    标题: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
    goselect * , 
           dbo.f_pid1(id) [路径(从父到子)] ,
           dbo.f_pid2(id) [路径(从子到父)]
    from tb order by iddrop 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 SERVER 2005中查询指定节点及其所有父节点的方法(字符串形式显示)
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2010-02-02
    地点:新疆乌鲁木齐
    */create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
    insert into tb values('001' , null  , N'广东省')
    insert into tb values('002' , '001' , N'广州市')
    insert into tb values('003' , '001' , N'深圳市')
    insert into tb values('004' , '002' , N'天河区')
    insert into tb values('005' , '003' , N'罗湖区')
    insert into tb values('006' , '003' , N'福田区')
    insert into tb values('007' , '003' , N'宝安区')
    insert into tb values('008' , '007' , N'西乡镇')
    insert into tb values('009' , '007' , N'龙华镇')
    insert into tb values('010' , '007' , N'松岗镇')
    go;with t as
    (
        select id , pid = id from tb 
        union all
        select t.id , pid = tb.pid from t inner join tb on t.pid = tb.id

    select id , 
           [路径(从父到子)] = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
           [路径(从子到父)] = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
    from tb
    group by id
    order by id
    /*
    id   路径(从父到子)   路径(从子到父)
    ---- ---------------  ---------------
    001  001              001
    002  001,002          002,001
    003  001,003          003,001
    004  001,002,004      004,002,001
    005  001,003,005      005,003,001
    006  001,003,006      006,003,001
    007  001,003,007      007,003,001
    008  001,003,007,008  008,007,003,001
    009  001,003,007,009  009,007,003,001
    010  001,003,007,010  010,007,003,001(10 行受影响)
    */
    ;with t as
    (
        select id , name , pid = id , path = cast(name as nvarchar(100)) from tb 
        union all
        select t.id , t.name , pid = tb.pid , path = cast(tb.name as nvarchar(100)) from t join tb on tb.id = t.pid 
    )
    select id , 
           name ,
           [路径(从父到子)_1] = pid1, 
           [路径(从父到子)_2] = reverse(substring(reverse(path1) , charindex(',' , reverse(path1)) + 1 , len(path1))) ,
           [路径(从子到父)_1] = pid2,
           [路径(从子到父)_2] = substring(path2 , charindex(',' , path2) + 1 , len(path2)) from
    (
    select id , name ,
           pid1 = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
           pid2 = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
           path1 = STUFF((SELECT ',' + path FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
           path2 = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
    from tb
    group by id , name
    ) m
    order by id
    /*
    id   name    路径(从父到子)_1  路径(从父到子)_2             路径(从子到父)_1  路径(从子到父)_2
    ---- ------  ----------------  ---------------------------  ----------------  ---------------------------
    001  广东省  001               广东省                       001               广东省
    002  广州市  001,002           广东省,广州市                002,001           广州市,广东省
    003  深圳市  001,003           广东省,深圳市                003,001           深圳市,广东省
    004  天河区  001,002,004       广东省,广州市,天河区         004,002,001       天河区,广州市,广东省
    005  罗湖区  001,003,005       广东省,深圳市,罗湖区         005,003,001       罗湖区,深圳市,广东省
    006  福田区  001,003,006       广东省,深圳市,福田区         006,003,001       福田区,深圳市,广东省
    007  宝安区  001,003,007       广东省,深圳市,宝安区         007,003,001       宝安区,深圳市,广东省
    008  西乡镇  001,003,007,008   广东省,深圳市,宝安区,西乡镇  008,007,003,001   西乡镇,宝安区,深圳市,广东省
    009  龙华镇  001,003,007,009   广东省,深圳市,宝安区,龙华镇  009,007,003,001   龙华镇,宝安区,深圳市,广东省
    010  松岗镇  001,003,007,010   广东省,深圳市,宝安区,松岗镇  010,007,003,001   松岗镇,宝安区,深圳市,广东省(10 行受影响)
    */drop table tb
      

  2.   


    --> 测试数据:[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
    */
    --到这一步了,不明白比还要怎么处理,怎么选择
      

  3.   

    create table #area
    (ID int,
    Name varchar(20),
    Parentid int)insert into #area(ID,Name,Parentid) values(1,'北京',0)
    insert into #area(ID,Name,Parentid) values(2,'广州',0)
    insert into #area(ID,Name,Parentid) values(3,'保定',1)
    insert into #area(ID,Name,Parentid) values(4,'同要',3)
    insert into #area(ID,Name,Parentid) values(5,'5街',4)
    insert into #area(ID,Name,Parentid) values(6,'海珠区',2)
    insert into #area(ID,Name,Parentid) values(7,'红海',6)
    insert into #area(ID,Name,Parentid) values(8,'中川路',7)
    insert into #area(ID,Name,Parentid) values(9,'红海江',6)
    insert into #area(ID,Name,Parentid) values(10,'中川工',6)
    insert into #area(ID,Name,Parentid) values(11,'中川',4)select * from #areadeclare @firstname varchar(20)
    declare @lastname varchar(20)
    select @firstname = '广州'
    select @lastname = '中川'declare @name varchar(20),@Parentid int
    declare @level int
    declare @result varchar(100)
    ----用游标取出需要处理的最低一层的数据
    declare cur_area cursor
    for 
    select name,Parentid from #area where Name like '%'+@lastname+'%'
    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 #area where ID = @Parentid
    set @level = @level -1
    end
    ----如果循环计算出来的最上层和输入一致,就显示出来
    ---这里也可以用临时表先存储数据,在游标结束后用select把结果一起调出来
    if @name = @firstname
    begin
    select @result
    end

    fetch cur_area into @name,@Parentid
    end
        
    close cur_area
    deallocate cur_area
      

  4.   

    我新建了一个newtemp临时表存储if @name = @firstname
            begin
                select @result
            end
    这里的数据,显示出来不对
      

  5.   

    改了一下,你看看
    如果基础数据很大,而结果集比较小,可以用表变量的方式提高性能declare @firstname varchar(20)
    declare @lastname varchar(20)
    select @firstname = '广州'
    select @lastname = '中川'
    ----声明表变量
    declare  @result_t table
    (result varchar(100))declare @name varchar(20),@Parentid int
    declare @level int
    declare @result varchar(100)
    ----用游标取出需要处理的最低一层的数据
    declare cur_area cursor
        for 
        select name,Parentid from #area where Name like '%'+@lastname+'%'
        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 #area where ID = @Parentid
                    set @level = @level -1
                end
            ----如果循环计算出来的最上层和输入一致,就把数据插入表变量@result_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_area
    ---查询结果
    select * from @result_t
      

  6.   

    我在应用asp和.net中调用了不显示数据,在分析器里面执行是对的呢,没明白是那里出问题。
      

  7.   


    ---建立实体表
    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)
    as
    begin
    declare @name varchar(20),@Parentid int
    declare @level int
    declare @result varchar(100)
    ----声明表变量
    declare  @result_t table
    (result varchar(100))----用游标取出需要处理的最低一层的数据
    declare cur_area cursor
        for 
        select name,Parentid from tb_area where Name like '%'+@lastname+'%'
        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
            ----如果循环计算出来的最上层和输入一致,就把数据插入表变量@result_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_t
    end----在程序里面调用存储过程获得数据
    exec up_find_area '广州','中川'
      

  8.   

    特别感谢prcak47一直的帮助。谢谢!