亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 初學(xué)Sybase,遇到一個遞歸查詢的問題,求解!請各位大俠幫幫忙哦! [打印本頁]

作者: 61412093    時間: 2013-05-09 09:41
標(biāo)題: 初學(xué)Sybase,遇到一個遞歸查詢的問題,求解!請各位大俠幫幫忙哦!
Sybase數(shù)據(jù)庫中如何實(shí)現(xiàn)以下業(yè)務(wù)需求。恳簿褪沁f歸查詢。但在Sybase中貌似沒有內(nèi)置的遞歸函數(shù),應(yīng)該只能用存儲過程來實(shí)現(xiàn)吧。
小弟不才,請問這種Sybase遞歸查詢的存儲過程怎么寫?

T_ORG_INFO(機(jī)構(gòu)表):
id  orgCode(機(jī)構(gòu)號)  orgName(機(jī)構(gòu)名稱)   pid(父ID)
1        00001                  機(jī)構(gòu)1(根機(jī)構(gòu))                null
2        00002                  機(jī)構(gòu)2                        1
3        00003                  機(jī)構(gòu)3                        1
4        00004                  機(jī)構(gòu)4                        2
5        00005                  機(jī)構(gòu)5                        4
6        00006                  機(jī)構(gòu)6                        4

需求:
如果查詢的是機(jī)構(gòu)號‘00002’的數(shù)據(jù),則需要實(shí)現(xiàn)兩種可能性的數(shù)據(jù)查詢(可以分別寫兩個SQL或存儲過程的語句):
1、需要查詢出它以及它的所有下級機(jī)構(gòu)信息(注:如果當(dāng)前查詢的機(jī)構(gòu)號是‘葉子機(jī)構(gòu)’(如:00005和00006)則沒有下級機(jī)構(gòu),默認(rèn)查詢當(dāng)前查詢的機(jī)構(gòu)信息即可)
2、需要查詢出它以及它的所有上級機(jī)構(gòu)信息(注:如果當(dāng)前查詢的機(jī)構(gòu)號是‘根機(jī)構(gòu)’(如:00001)則沒有上級機(jī)構(gòu),默認(rèn)查詢當(dāng)前查詢的機(jī)構(gòu)信息即可)

如上條件(00002)所述,查詢出來的數(shù)據(jù)應(yīng)該是:
1、本機(jī)構(gòu)以及所有下級機(jī)構(gòu):‘00002’、‘00004’、‘00005’和‘00006’的信息。
2、本機(jī)構(gòu)以及所有上級機(jī)構(gòu):‘00002’和‘00001’的信息。

請各位DX給個存儲過程出來!最好能有注釋哦,拜謝啦。
作者: baiynije    時間: 2013-05-10 11:39
本帖最后由 baiynije 于 2013-05-10 11:40 編輯

這個不一定要用遞歸啊,你可以用個循環(huán)來處理,
比如要取上級機(jī)構(gòu)可以這樣
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
作者: mfkpie8    時間: 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
復(fù)制代碼





歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2