在SQL
在庫爾勒等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),庫爾勒網(wǎng)站建設(shè)費用合理。
SERVER
2000
中你可以先一些自定義函數(shù),或一些存儲過程,實現(xiàn)遞歸:
select
level,TypeName
from
ProductType
t
START
WITH
t.ParentID=0
CONNECT
BY
PRIOR
t.ProductTypeID=
t.ParentID;
CREATE TABLE #tb1(stuId INT,stuName VARCHAR(30),teaId INT);
INSERT INTO #tb1 (stuId,stuName,teaId)
VALUES(1,'zhou',0),(2,'kong',0),(3,'hong',2),(4,'zhang',1),(5,'liu',4),
(6,'zhao',5),(7,'zheng',6),(8,'wei',7)
;WITH cte AS (
SELECT t.stuId,t.stuName,t.teaId FROM #tb1 AS t
WHERE t.stuId=8
UNION ALL
SELECT t.stuId,t.stuName,t.teaId FROM cte AS c
JOIN #tb1 AS t ON c.teaId=t.stuId
)
SELECT * FROM cte
寫在SQL里吧,可以避免頻繁的創(chuàng)建連接,那是開銷很大的.
不過可能寫在sql里比較煩吧.
with district as ( select * from SECFUNMODULE where MENUID='030' union all select a.* from SECFUNMODULE a, district b where a.PMENUID = b.MENUID ), district1 as ( select a.* from district a where a.MENUID in (select PMENUID from district) ) select * from district1 order by MENUID with當(dāng)然可以實現(xiàn). 請參照sqlserver2005 的遞歸查詢
set?@p_b?=?(select?fid?from?w_Storage_Depository?where?fid?in(select?fid?from?dbo.jt_base_company?where?bj=1))
改成
select??@p_b?=fid?from?w_Storage_Depository?where?fid?in(select?fid?from?dbo.jt_base_company?where?bj=1)
WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS ( SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL UNION ALL --主要是在此,增加一個union all SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 FROM dbo.MyEmployees AS e INNER JOIN DirectReports AS d -- 在此調(diào)用自身,就可以實現(xiàn)遞歸了 ON e.ManagerID = d.EmployeeID ) SELECT ManagerID, EmployeeID, Title, EmployeeLevel FROM DirectReports ORDER BY ManagerID;