Hello i have tree structure in sql. Logics is standard: SomeID, ParentID, other fields. I have select procedure, which selects data like this:
1.
1.1
1.1.1
and so on.
How to write the select procedure, to get the inverted result(first are selected the deepest branches, last - root branches), like this:
1.1.1.
1.1.
1.
2.2.2.2.2.
2.2.2.2.
2.2.2.
2.2.
2.
and so on.
Non inversive select looks like thi(I use SqlServer 2008)s:
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