Use SQL to export parent / child strings to a flat file

With table Orders:
(OrderID, date, customerID, status, etc)

and OrderDetails:
(ParentID, itemID, quantity, price, etc)

I would like to create a SQL query that will export a CSV file with alternating strings Orderand OrderDetail. For example, the output might look like this ( Hand Dindicate "Title" and "Detail, respectively.):

"H",2345,"6/1/09",856,"Shipped"
"D",2345,52,1,1.50
"D",2345,92,2,3.25
"D",2345,74,1,9.99
"H",2346,"6/1/09",474,"Shipped"
"D",2346,74,1,9.99
"D",2346,52,1,1.50

I don’t know where to start. Any ideas? TIA.

+3
source share
1 answer

, union all order by . , (2!) , (1!) , , .

, , . , CSV, . union all . null, , '', , null CSV.

select
    'H',
    OrderID,
    Date,
    CustomerID,
    Status
from
    Headers
union all
select
    'D',
    ParentID,
    ItemID,
    Quantity,
    Price
from
    Details
order by
    2 asc, 1 desc
+5

Source: https://habr.com/ru/post/1712825/


All Articles