您好,我在Oracle数据库中拥有这部分视图,并且必须在Microsoft Sql Server上对其进行更改。更改选择连接通过事先从Oracle到SQL Server
with V_LOCHIERARHY_N
(nr, nivel, location, parent, systemid, siteid, orgid, count_a, count_wo, children)
AS
SELECT LEVEL, LPAD (' ', 2 * (LEVEL - 1)) || l.LOCATION nivel,
LOCATION, PARENT, systemid, siteid, orgid,
(SELECT COUNT (a.ancestor)
FROM locancestor a
WHERE a.LOCATION = l.LOCATION AND a.siteid = l.siteid),
NVL (COUNT (w.wonum), 0)
FROM maximo.workorder w
WHERE ( w.reportdate >
TO_TIMESTAMP ('2006-06-19 00:00:01',
'YYYY-MM-DD HH24:MI:SS.FF'
)
AND w.istask = 0
AND w.worktype <> 'P'
AND w.LOCATION = l.LOCATION
)
AND w.status <> 'CAN'),
l.children
FROM lochierarchy l
START WITH l.LOCATION = 'StartPoint'
CONNECT BY PRIOR l.LOCATION = l.PARENT AND l.siteid = 'SiteTest'
我需要从这个脚本中返回一个给定条目的所有子节点(可以在位置表中找到的子节点的描述)。
我有下一列的表:
Location Parent Systemid Children Siteid Origid Lochierarchyid
A001 StartPoint Primary 2 SiteTest X 106372
A002 A001 Primary 2 SiteTest X 105472
A003 A002 Primary 0 SiteTest X 98654
A004 A002 Primary 1 SiteTest X 875543
A004B A004 Primary 0 SiteTest X 443216
B005 StartPoint Primary 0 SiteTest X 544321
例如,对于给定的条目A001将返回
A002
A003
A004
A004B
B005
我做下面这个观点,但我不知道如何将其与第一个整合。此外,它不会返回我的列表中corectly为了
Parent
Children 1 of parent
Children a of children 1
children b of children 1
children 2 of parent
children a1 of children 2 and so on.
WITH testCTE AS
(
SELECT l.parent, l.location as child, l.location, l.lochierarchyid
FROM lochierarchy l
where location='SecondLocation' --and siteid='SiteTest'
UNION ALL
SELECT c.Parent, l.parent, l.location, l.lochierarchyid
FROM lochierarchy l
INNER JOIN testCTE c ON l.parent = c.location
)
SELECT *
FROM testCTE c
order BY c.parent,child asc
;
可以请人帮助我吗? :)
如果我有一段时间,我会努力寻找解决方案。你也可以自己解决,并提供一些帮助。下面的文章逐步展示了如何通过递归子查询分解重现“connect by”查询的所有功能:https://oracle-base.com/articles/11g/recursive-subquery-factoring-11gr2 – mathguy
请做*不*破坏你的帖子。 –
[如何从SQL Server中的SELECT进行更新?](http://stackoverflow.com/questions/2334712/how-to-update-from-a-select-in-sql-server) – Madalina