I'm trying to not only query different tables in my database and export them into a CSV, but also export data from multiple tables and concatenate them as columns into this CSV.
I start with the premise of being able to spool the contents of a table into a CSV file:
set colsep ','
set echo off
set feedback off
set linesize 1000
set sqlprompt ''
set headsep off
spool file.csv
select * from messages;
Now I want to grab certain components of multiple tables with no avail (with union and minus):
select col1, col2, col3 from messages
union
select col1, col2 from messagedata
union
select col1 from extradata;
In trying the above method, I'll get ORA -1789 query block has incorrect number of result columns
meaning that I'm trying to match up the wrong number of columns (which I'm trying to avoid by just appending columns in my output).
And given either union or minus it will error on the wrong datatype, but that being because it's just trying to append it to the columns from my first query.
My goal there is to create a query that produces similar output:
| col1(messages) | ... | col2(messagedata) | ... | col3(extradata) |
-----------------------------------------------------------------------
message ,... ,messagedata ,... ,extradata
message ,... ,messagedata ,... ,extradata
message ,... ,messagedata ,... ,extradata
So two questions:
Is it possible for me to concatenate the data from multiple tables into a column output like above? As in create new columns to output instead of appending to existing columns?
How might I give a header to my data (custom or already defined by the identifier in the table) at the top of the column?
JOIN
these table rather thanUNION
. Now how are the tables related to each other?JOIN
them using row number for example.