Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to MS SQL 'FOR XML PATH' in other database products?

Tags:

sql

xml

I am currently using Microsoft's FOR XML PATH feature to pull data back from MS SQL Server in an XML format. I really like the feature but am always uncomfortable about using vendor specific functions.

Do any of the other major database vendors have something similar?

Edit

Let me be more specific with type of features I'm looking for

In MS SQL you can use this query

SELECT so.id AS '@Id',
       so.Code AS '@Code',  
       cu.Code AS 'Customer/@Code',
       cu.Name AS 'Customer/@Name',
       (SELECT Item_Num AS '@Item',
                pa.Code AS '@PartCode'
        FROM tblSalesItem si
            LEFT JOIN tblPart pa ON pa.Id = si.Part_Id
        WHERE si.SalesOrder_ID = so.Id 
        FOR XML PATH('SalesItem') , type) 
FROM tblSalesOrder so
    JOIN tblCustomer cu ON so.customer_id = cu.ID
FOR XML PATH('SalesOrder'), ROOT('SalesOrders')

to generate this XML

<SalesOrders>
  <SalesOrder Id="13" Code="C1002     ">
    <Customer Code="ROBERTS   " Name="Roberts Equipment  Inc." />
    <SalesItem Item="1" PartCode="FP-0001" />
    <SalesItem Item="2" PartCode="FP-0003" />
  </SalesOrder>
  <SalesOrder Id="15" Code="C1004     ">
    <Customer Code="EXBEL-LIFTS" Name="Exbel Lifts Inc." />
    <SalesItem Item="1" />
  </SalesOrder>
</SalesOrders>

I can control the use of elements and attributes on a column by column basis. I can create attributes of elements to group related columns together. I can use sub-queries to generate nested elements. You can even use functions that return XML to generate trees that are arbitrary depth.

like image 205
Darrel Miller Avatar asked Mar 20 '09 02:03

Darrel Miller


2 Answers

FOR XML PATH is not in the ANSI SQL-92 standard, so it will be up to the individual vendors. I do not know of any equivalent in Oracle or MySQL.

like image 135
Mitch Wheat Avatar answered Sep 27 '22 18:09

Mitch Wheat


PostgreSQL 8.3 does not specifically have FOR XML PATH, but its XML functions (see section 9.14 of the manual, particularly 9.14.3) appear to provide very similar functionality.

like image 30
kquinn Avatar answered Sep 27 '22 17:09

kquinn