免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 21234 | 回复: 2
打印 上一主题 下一主题

初学Sybase,遇到一个递归查询的问题,求解!请各位大侠帮帮忙哦! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-09 09:41 |只看该作者 |倒序浏览
Sybase数据库中如何实现以下业务需求啊?也就是递归查询。但在Sybase中貌似没有内置的递归函数,应该只能用存储过程来实现吧。
小弟不才,请问这种Sybase递归查询的存储过程怎么写啊?

T_ORG_INFO(机构表):
id  orgCode(机构号)  orgName(机构名称)   pid(父ID)
1        00001                  机构1(根机构)                null
2        00002                  机构2                        1
3        00003                  机构3                        1
4        00004                  机构4                        2
5        00005                  机构5                        4
6        00006                  机构6                        4

需求:
如果查询的是机构号‘00002’的数据,则需要实现两种可能性的数据查询(可以分别写两个SQL或存储过程的语句):
1、需要查询出它以及它的所有下级机构信息(注:如果当前查询的机构号是‘叶子机构’(如:00005和00006)则没有下级机构,默认查询当前查询的机构信息即可)
2、需要查询出它以及它的所有上级机构信息(注:如果当前查询的机构号是‘根机构’(如:00001)则没有上级机构,默认查询当前查询的机构信息即可)

如上条件(00002)所述,查询出来的数据应该是:
1、本机构以及所有下级机构:‘00002’、‘00004’、‘00005’和‘00006’的信息。
2、本机构以及所有上级机构:‘00002’和‘00001’的信息。

请各位DX给个存储过程出来啊!最好能有注释哦,拜谢啦。

论坛徽章:
0
2 [报告]
发表于 2013-05-10 11:39 |只看该作者
本帖最后由 baiynije 于 2013-05-10 11:40 编辑

這個不一定要用遞歸啊,你可以用個循環來處理,
比如要取上級機構可以這樣
CREATE PROCEDURE dbo.get_uporg_sp
                    @orgcode varchar(10)
AS
declare @uporgcode varchar(10),@pid int

create table #tab1(orgcode varchar(10) not null)

select @pid = pid from T_ORG_INFO where orgcode = @orgcode
while @pid is not null
    begin
        select @uporgcode = orgcode from T_ORG_INFO where id = @pid
        insert into #tab1(orgcode) values(@uporgcode)
           
        select @pid = pid from T_ORG_INFO where id = @pid     
    end

select * from #tab1

论坛徽章:
0
3 [报告]
发表于 2013-07-30 22:51 |只看该作者
  1. ALTER PROCEDURE "DBA"."UPS_PRODUCT_CODE_SELECT_CHILD_ID"(@arg_id char(30))
  2. as
  3. begin transaction
  4. declare @count integer
  5. declare @ic integer
  6. declare @Version char(30)
  7. select id=@arg_id,pross_flag='Y',parent_id='' into #temp_table
  8.   from product_code where product_code = @arg_id
  9. if(@@Error <> 0) goto OnError
  10. select id=product_code,pross_flag='N',parent_id=parent_id into #temp_table2
  11.   from product_code where ltrim(isnull(parent_id,'')) = @arg_id
  12. if(@@Error <> 0) goto OnError
  13. insert into #temp_table(id,pross_flag,parent_id)
  14.   select id,'N',parent_id from #temp_table2
  15. if(@@Error <> 0) goto OnError
  16. delete from #temp_table2
  17. if(@@Error <> 0) goto OnError
  18. select @count=1
  19. while(@count >= 1)
  20.   begin
  21.     insert into #temp_table2(id,pross_flag,parent_id)
  22.       select product_code.product_code,'N',product_code.parent_id from
  23.         product_code where ltrim(isnull(product_code.parent_id,'')) <> '' and product_code.parent_id = any(select #temp_table.id from #temp_table where pross_flag = 'N')
  24.     if(@@Error <> 0) goto OnError
  25.     update #temp_table set pross_flag = 'Y'
  26.     if(@@Error <> 0) goto OnError
  27.     insert into #temp_table(id,pross_flag,parent_id)
  28.       select id,pross_flag,parent_id from #temp_table2
  29.     if(@@Error <> 0) goto OnError
  30.     delete from #temp_table2
  31.     if(@@Error <> 0) goto OnError
  32.     select @count = isnull(COUNT(*),0) from #temp_table where pross_flag = 'N'
  33.   end
  34. select isnull(#temp_table.id,'') from
  35.   #temp_table where #temp_table.id <> ''
  36. OnError: if(@@Error <> 0)
  37.   begin
  38.     rollback transaction
  39.   end
  40. else
  41.   begin
  42.     commit transaction
  43.   end
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP