안녕하세요, 저는 SQL에서 트리 구조를 가지고 있습니다. 논리는 표준입니다 : SomeID, ParentID, 다른 필드. 이 같은 데이터를 선택하는 선택 절차가 있습니다 등등SQL 반전 트리 선택
1.
1.1
1.1.1
하고 있습니다. 그래서
1.1.1.
1.1.
1.
2.2.2.2.2.
2.2.2.2.
2.2.2.
2.2.
2.
를 실행 한 다음과 같이 - 반전 된 결과를 얻을 수, 선택 프로 시저를 작성하는 방법
은 (루트 가지 먼저 가장 깊은 지점, 마지막 선택). 생 (내가 SQLSERVER 2008을 사용)의 같은비 inversive 선택 외모 : 당신이 재귀를 할 수없는 경우
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Object_SelectDownByRoot]
@ObjectID int
AS
WITH tree (ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName) AS
(
SELECT
ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName
FROM dbo.[ObjectQ] ofs
WHERE(ObjectID = @ObjectID)
UNION ALL
SELECT ofs.ObjectID, ofs.ParentID, ofs.ObjectName, ofs.ObjectCode, ofs.DistrictID, ofs.DistrictName,
ofs.CityName, ofs.RegionName, ofs.StreetName, ofs.StreetID, ofs.AddressID, ofs.ObjectTypeName,
ofs.RouteName, ofs.ObjectTypeID, ofs.RouteID, ofs.AvrgTempIn, ofs.Area, ofs.Volume,
ofs.ElectricPower, ofs.ObjectStatusName, ofs.ObjectStatusID, ofs.[ControlRoom?], ofs.DateBuild,
ofs.[Floor], ofs.EncloseName, ofs.EncloseID, ofs.MaintenanceEval, ofs.AdministratorID,
ofs.Administrator, ofs.ElectricityPerson, ofs.ElectricityPersonID,
ofs.HeatingPersonID, ofs.HeatingPerson, ofs.HouseNo, ofs.FlatNo, ofs.ZIP,
ofs.AddressStreet, ofs.RouteCode, ofs.RouteDescription,
ofs.AddressDescription, ofs.StreetID2, ofs.CityID, ofs.AddressCityName
FROM dbo.[ObjectQ] ofs
JOIN tree ON tree.ObjectID = ofs.ParentID
)
SELECT
ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName
FROM tree
regualar (비역) 트리를 선택하는 데 사용하는 쿼리를 게시 할 수 있습니까? 어떤 데이터베이스를 사용하고 있습니까? 기술은 데이터베이스마다 다르지만 재귀 쿼리에 대해 Google 검색을 수행하십시오. – FrustratedWithFormsDesigner
게시물을 편집하여 원래 쿼리를 게시했습니다. – Vytas999
원래 쿼리에서 중요하지 않은 필드를 제거하면 도움이 될 수 있습니다. 그것은 당신이 1, 1.1, 1.1.1 등을 어디에서 얻고 있는지 명확히 할 것입니다. –