Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting MSSQL 'FOR XML PATH' To Oracle

Tags:

I have a Statement to fill a table on my MSSQL Database. It joins some values together seperated by a semicolon.

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 

I don't have enough Knowledge to convert this to Oracle. It should give me the same Output as from MSSQL. Can someone help me?

EDIT:

Here is some sample data:

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

And here is my desired Output:

ArcDocINr | SWorte
----------|---------
1         | Müller;100
2         | 111111;13579;002
3         | TM-AH
like image 231
F. Baum Avatar asked Jul 06 '17 06:07

F. Baum


People also ask

What does for XML Path do in SQL?

We can use FOR XML PATH to prepare a comma-separated string from the existing data. Let's create an Authors table and insert a few records into it. In the data, we can see we have an ID column and the AuthorName column. If we just select the records, it gives the output in the following format.

Does Oracle support XML?

Oracle XML DB provides full support for all of the key XML standards, including XML, Namespaces, DOM, XQuery, SQL/XML and XSLT. By providing full support for XML standards, Oracle XML DB supports native XML application development.

How do I import XML into Oracle SQL Developer?

Import an XML File into Oracle Table Using Oracle SQL Developer. First, convert your XML file to CSV, by clicking on the following link Convert XML to CSV. Paste your XML file contents into the text box of the website, and then you would be able to download the CSV file.


1 Answers

Use LISTAGG:

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

Update:

If you are inserting into the XXAArcDoc table using values from the XXAArcSW table then something like:

INSERT INTO XXAArcDoc ( ArcDocINr, SWorte )
SELECT ArcDocINr,
       LISTAGG( SWort, ';' ) WITHIN GROUP ( ORDER BY ArcSWINr )
FROM   XXAArcSW
GROUP BY ArcDocINr
like image 121
MT0 Avatar answered Oct 11 '22 09:10

MT0