Loop variable SQL query to append tables - sql

I have a database that has 100+ tables, all with the same header. I want to merge these tables into one. Also within the database is a table that lists all the other tables (an inventory of the database per se).
I'm looking for a way to loop the following SQL append query so VaryingTableName changes to follow through my inventory table:
INSERT INTO MainTable IN 'C:\newDBFile.accdb'
SELECT VaryingTableName.*
FROM VaryingTableName;
If there were a way to do this without the inventory table, that's fine too.

It's not the most pretty solution and involves no automation but you could do:
INSERT INTO MainTable (Col1, Col2, Col3, Col4) IN 'C:\newDBFile.accdb'
SELECT Col1, Col2, Col3, Col4
FROM (
SELECT Col1, Col2, Col3, Col4
FROM OldTable1
UNION ALL
SELECT Col1, Col2, Col3, Col4
FROM OldTable2
...)

Related

Get one row per unique column value combination (`DISTINCT ON` operation without using it)

I have a table with 5 columns, For each unique combination of the first three columns, I want a single sample row. I don't care which values are considered for columns 4 and 5, as long as they come from the same row (they should be aligned).
I realise I can do a DISTINCT to fetch on the first three columns to fetch unique combinations of the first three columns. But the problems is then I cannot get 4th and 5th column values.
I considered GROUP BY, I can do a group by on the first three columns, then I can do a MIN(col4), MIN(col5). But there is no guarantee that values of col4 and col5 are aligned (from the same row).
The DB I am using does not support DISTINCT ON operation, which I realise is what I really need.
How do I perform what DISTINCT ON does without actually using that operation ?
I am guessing this is how I would write the SQL if DISTINCT ON was supported:
SELECT
DISTINCT ON (col1, col2, col3)
col1, col2, col3, col4, col5
FROM TABLE_NAME
select
col1, col2, col3, col4, col5
from (
select col1, col2, col3, col4, col5,
row_number() over (partition by col1, col2, col3) as n
from table_name
)
where n = 1

pl/sql insert from select

I have tow tables:
table1 with 5 columns
table2 with 4 columns
I want to copy the data from table2 to table1.
In table1 I have column with default value, let's say column2 ='default'
How i do it in PL/SQL?
You can insert and enumerate the target columns. The idea is:
insert into table1(col1, col3, col4, col5)
select col1, col2, col3, col4 from table2
The "additional" column of table1 (I assumed col2) is not part of the list of columns to insert into - so Oracle will happily use the configured default value.
You can simply use the constant value for the col2 as follows:
insert into table1(col1, col2, col3, col4, col5)
select col1, 'default', col2, col3, col4 from table2;

PL/SQL Inserting into a table minus another table but inserting another column at the same time

I have a small problem, say I have a table with 5 columns, I insert into that table using MINUS from another table that has 4 columns.
I am having problem inserting a (null) or ' ' value into the 5th column when I do that statement as it says:
invalid number of columns selected (table I am minusing from does not have the 5th column)
This is my code I use for the insert into statement
INSERT INTO my_table(SELECT col1, col2, col3, col4 FROM that_table
MINUS SELECT col1, col2, col3, col4 FROM my_table);
This code works, it copies whatever I need if I don't create a 5th column, is there a way to insert into my table values from the other table alongside a 5th column in my_table?
Thank you
Assuming I'm understanding your question, you want to select all records from that_table and insert those into my_table where the first 4 columns don't exist, using null as the value for the 5th column? If so, you can use not exists:
insert into my_table (col1, col2, col3, col4, col5)
select col1, col2, col3, col4, null
from that_table tt
where not exists (
select 1
from my_table mt
where tt.col1 = mt.col1 and tt.col2 = mt.col2
and tt.col3 = mt.col3 and tt.col4 = mt.col4
)
You should also be able to use Minus, just make sure you have the same number of columns and same data types:
insert into my_table (col1, col2, col3, col4, col5)
select col1, col2, col3, col4, null
from that_table
minus
select col1, col2, col3, col4, null
from my_table
If you want to insert only the selected columns, then try this:
insert into my_table (col1, col2, col3, col4, col5) select col1, col2, col3, col4, null from that_table;
You can find more information at http://www.w3schools.com/sql/sql_insert_into_select.asp

Have unpivot automatically grab column list (oracle 11g)

This is a follow up question to Transpose one row into many rows Oracle
I want to be able to unpivot an arbitrary query result.
To unpivot a table manually, I would do:
select value_type, value from (
(
-- query to be unpivoted
-- EG: select col1, col2, col3, col4, col5 from table
)
unpivot
(
-- Line I would like to change
value for value_type in (col1, col2, col3, col4, col5)
)
);
This works for all queries that return 5 columns, called col1, col2, etc. Is there something I put in instead of value for value_type in (col1, col2, col3, col4, col5) that will grab all the column names from the query/view/table that is selected in the first part?
You could create a stored procedure to do this in PL/SQL by dynamically creating your SQL statement as a string an then using execute immediate to execute it and return a cursor.

join two tables into one big table

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:
SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2
However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).
May it work to just do:
SELECT col1, col2, col3
INTO Table1
FROM Table2
Start with union all:
select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2
Your query is trying to deduplicate things, which would slow it down considerably.
I think the best option is to create a view in sql server, this will optimize the performance of the query:
SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2
(As other users said: "union" is used to select distinct values from two tables
where as "union all" is used to select all values including
duplicates from the tables.)
At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.
You could use this to fill the second table:
Insert into table2 select * from table1;
Or if you want to be more specific:
Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;
(Note: some DBMSs might require putting parenthesis around the SELECT clause.)
select * into new table(your new table name)
from table1.col1,table1.col2,table2.col1;
here columns can be your required columns .
select * into newtable from table1
union all
select * from table2
Worked well. Guidelines, both tables have exact same column names :)