转换MSSQL' FOR XML PATH'致Oracle

时间:2017-07-06 06:28:48

标签: sql sql-server oracle code-conversion for-xml-path

我有一个声明来填写MSSQL数据库的表格。它将一些值加在一起,用分号分隔。

INSERT INTO XXAArcDocSWSB (ArcDocINr, SWorte)
SELECT A.ArcDocINr, B.SWorte FROM XXAArcDoc A 
LEFT JOIN (
SELECT DISTINCT T2.ArcDocINr,
SUBSTRING(
    (
        SELECT ';' + T1.SWort  AS [text()]
        FROM (SELECT D.ArcDocINr, SW.SWort FROM XXAArcDoc D, XXAArcSW SW WHERE D.ArcDocINr = SW.ArcDocINr) T1
        WHERE T1.ArcDocINr = T2.ArcDocINr
        For XML PATH ('')
    ), 2, 255) [SWorte]
FROM (SELECT D.ArcDocINr, SW.SWort FROM XXAArcDoc D, XXAArcSW SW WHERE D.ArcDocINr = SW.ArcDocINr) T2
) B ON A.ArcDocINr = B.ArcDocINr 

我没有足够的知识将其转换为Oracle。它应该给我与MSSQL相同的输出。 有人能帮助我吗?

编辑:

以下是一些示例数据:

XXAArcDoc:

ArcDocINr | ...
----------|----------
1         |
2         |
3         |
.         |
.         |
.         |

XXAArcSW:

ArcSWINr | ArcDocINr | SWort
---------|-----------|---------
6        | 1         | Müller
7        | 1         | 100
8        | 2         | 111111
9        | 2         | 13579
10       | 2         | 002
11       | 3         | TM-AH

这是我想要的输出:

ArcDocINr | SWorte
----------|---------
1         | Müller;100
2         | 111111;13579;002
3         | TM-AH

1 个答案:

答案 0 :(得分:2)

使用LISTAGG

SELECT ArcDocINr,
       LISTAGG(
          SWort,
          ';'
       ) WITHIN GROUP ( ORDER BY ArcSWINr ) AS SWorte
FROM   XXAArcSW
GROUP BY ArcDocINr;

<强>更新

如果使用XXAArcDoc表中的值插入XXAArcSW表,则类似:

INSERT INTO XXAArcDoc ( ArcDocINr, SWorte )
SELECT ArcDocINr,
       LISTAGG( SWort, ';' ) WITHIN GROUP ( ORDER BY ArcSWINr )
FROM   XXAArcSW
GROUP BY ArcDocINr