This will give you only the first level of hierarchy,
If you want to go down or up in the herarchy, you need to use a recursive query with a common table expression. Use it in a table-valued function.
Going up : (root uuid represent the uuid of the "lowest" CI)
CREATE FUNCTION [dbo].[fn_getCIHierarchyUp]
(
@rootCIUUID binary(16)
)
RETURNS TABLE
AS
RETURN
(
--create a CTE to use with the recursivity
With ciHierarchy(childUUID,relType,parentUUID)
as
(
--select in the relationship table
Select BMHIER.hier_child, BMHIER.ci_rel_type, BMHIER.hier_parent
from mdb.dbo.busmgt BMHIER
where BMHIER.hier_child = @rootCIUUID
UNION ALL
--union with another select in the relationship table but with a join on the CTE
Select BMHIER.hier_child, BMHIER.ci_rel_type, BMHIER.hier_parent
from mdb.dbo.busmgt BMHIER
inner join ciHierarchy BMHIERTEMP -- join on the CTE. Unleash the recursivity!
on BMHIERTEMP.parentUUID=BMHIER.hier_child
)
select * from ciHierarchy
)
Going down (root uuid represent the uuid of the highest CI)
CREATE FUNCTION [dbo].[fn_getCIHierarchyDown]
(
@rootCIUUID binary(16)
)
RETURNS TABLE
AS
RETURN
(
--create a CTE to use with the recursivity
With ciHierarchy(childUUID,relType,parentUUID)
as
(
--select in the relationship table
Select BMHIER.hier_child, BMHIER.ci_rel_type, BMHIER.hier_parent
from mdb.dbo.busmgt BMHIER
where BMHIER.hier_parent = @rootCIUUID
UNION ALL
--union with another select in the relationship table but with a join on the CTE
Select BMHIER.hier_child, BMHIER.ci_rel_type, BMHIER.hier_parent
from mdb.dbo.busmgt BMHIER
inner join ciHierarchy BMHIERTEMP -- join on the CTE. Unleash the recursivity!
on BMHIERTEMP.childUUID=BMHIER.hier_parent
)
select * from ciHierarchy
)
You can add a condition on the "rel_type" column to walk throught a specific type of relation!
Usage is as follow :
SELECT * FROM [dbo].[fn_getCIHierarchyXXXX] (0xAABBCCEEDDFF00112233445566778899)
Since it returns parent and child uuid, you can add two inner join on ca_owned_resource to get resource_name !
Hope this helps,