存儲(chǔ)過(guò)程里的遞歸 實(shí)現(xiàn)方法
更新時(shí)間:2006年12月28日 00:00:00 作者:
一個(gè)二叉樹(shù)的遞歸,找出左邊最下面的一個(gè)點(diǎn)
id 自動(dòng)編號(hào)
pid 父ID
id_path 節(jié)點(diǎn)路徑
flg 位置,0左邊,1右邊
ALTER PROCEDURE [dbo].[get_class]
其實(shí)只需一個(gè)查詢語(yǔ)句就能找出最下面的ID
節(jié)點(diǎn)路徑包函1,并且父節(jié)點(diǎn)位置都是左邊,取出最大一個(gè)
select top 1 * from class a where id_path like '%1,%' and flg=0 and (select flg from class where id=a.pid)=0 order by id desc
id 自動(dòng)編號(hào)
pid 父ID
id_path 節(jié)點(diǎn)路徑
flg 位置,0左邊,1右邊
ALTER PROCEDURE [dbo].[get_class]
復(fù)制代碼 代碼如下:
@class int,
@return int output
AS
SELECT @return=isnull(( select top 1 id from class where pid=@class and flg=0 ),-2)
if @return>-1
begin
exec get_class @return,@return output--這里為什么要用@return,你要最后得到這個(gè)值,必須把它傳進(jìn)去
end
else
begin
SELECT @return=@class
end
GO
@return int output
AS
SELECT @return=isnull(( select top 1 id from class where pid=@class and flg=0 ),-2)
if @return>-1
begin
exec get_class @return,@return output--這里為什么要用@return,你要最后得到這個(gè)值,必須把它傳進(jìn)去
end
else
begin
SELECT @return=@class
end
GO
其實(shí)只需一個(gè)查詢語(yǔ)句就能找出最下面的ID
節(jié)點(diǎn)路徑包函1,并且父節(jié)點(diǎn)位置都是左邊,取出最大一個(gè)
select top 1 * from class a where id_path like '%1,%' and flg=0 and (select flg from class where id=a.pid)=0 order by id desc
相關(guān)文章
SQL Server 存儲(chǔ)過(guò)程的分頁(yè)
SQL Server 存儲(chǔ)過(guò)程的分頁(yè)...2006-08-08
ASP 千萬(wàn)級(jí)數(shù)據(jù)分頁(yè)的存儲(chǔ)過(guò)程
經(jīng)測(cè)試,在 14483461 條記錄中查詢第 100000 頁(yè),每頁(yè) 10 條記錄按升序和降序第一次時(shí)間均為 0.47 秒,第二次時(shí)間均為 0.43 秒2008-11-11
asp sqlserver 執(zhí)行存儲(chǔ)過(guò)程返回記錄集報(bào)對(duì)象關(guān)閉時(shí)不允許操作
asp sqlserver 執(zhí)行存儲(chǔ)過(guò)程返回記錄集報(bào)對(duì)象關(guān)閉時(shí)不允許操作的臨時(shí)解決方法。大家有更好的方法,可以說(shuō)下。2009-08-08
ASP下存儲(chǔ)過(guò)程編寫(xiě)入門(mén)全接觸
ASP下存儲(chǔ)過(guò)程編寫(xiě)入門(mén)全接觸...2007-10-10
SQL SERVER編寫(xiě)存儲(chǔ)過(guò)程小工具
SQL SERVER編寫(xiě)存儲(chǔ)過(guò)程小工具...2006-08-08
ASP存儲(chǔ)過(guò)程開(kāi)發(fā)應(yīng)用詳解
ASP開(kāi)發(fā)中存儲(chǔ)過(guò)程應(yīng)用詳解|調(diào)用,參數(shù),存儲(chǔ),數(shù)據(jù)庫(kù),輸出,編譯,mycomm,輸入,userid,代碼 ASP與存儲(chǔ)過(guò)程(Stored Procedures)的文章不少,但是我懷疑作者們是否真正實(shí)踐過(guò)。2008-10-10
關(guān)于使用存儲(chǔ)過(guò)程創(chuàng)建分頁(yè)
關(guān)于使用存儲(chǔ)過(guò)程創(chuàng)建分頁(yè)...2006-08-08

