pl/sql insert from select - sql

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;

Related

How do I copy over data from one table to another which has new columns?

I have Table A and Table B.
Both used to have same content but now table A has a few extra non nullable columns. I am trying to copy over data from Table B into Table A but getting an error when I try this query,
INSERT INTO TABLE_A (SELECT * FROM TABLE_B);
This throws the error, ORA-00947: not enough values
INSERT INTO TABLE_A(Col1, Col2, Col3, Col4)
VALUES ((SELECT Col1 FROM TABLE_B), (SELECT Col3 FROM TABLE_B), 'New Column', 'New Column');
This throws the error, ORA-01427: single-row subquery returns more than one row
Try this:
INSERT INTO TABLE_A (Col1, Col2, Col3, Col4)
(SELECT Col1, Col3, 'New Column', 'New Column' FROM TABLE_B);
This should return all the rows from TABLE_B with the correct number of columns.
As you mentioned in question itself table structure of A and B are different.
This is the reason why INSERT INTO SELECT.. is failing.
Below is the query for reference:
INSERT INTO TABLE_A (Col1, Col2, Col3, Col4)
(
SELECT Col1, Col3, 'DUMMY', 'DUMMY'
FROM TABLE_B
);

How Can I Use the Max Function to Filter a List and Insert Into

Is there a way to do something like:
Insert Into (col1, col2, col3)
Select col1, col2, col3, max(col4)
From mytable
Group By col1, col2, col3
That gives me: The select list for the INSERT statement contains more items than the insert list.
I want to use the max function to filter out dupes but when I select this extra field, the order of fields and number of fields doesn’t match up. How can I filter a list from a table, use the max function, and insert all records except the ones in the max field?
I want to use the max function to filter out dupes
Well, I suspect that you actually want distinct:
insert into my_target_table(col1, col2, col3)
select distinct col1, col2, col3 from my_source_table
This will insert one record in the target table for each distinct (col1, col2, col3) tuple in the source table.
You are describing something like this:
Insert Into (col1, col2, col3)
select col1, col2, col3
from mytable
where t.col4 = (select max(t2.col4)
from mytable t2
where t2.col1 = t.col1 and t2.col2 = t.col2 and t2.col3 = t.col3
);
However, this is pretty much equivalent to select distinct (NULL values might be treated differently). You probably want dupes defined on only one column, so I'm thinking:
insert into (col1, col2, col3)
select col1, col2, col3
from mytable
where t.col4 = (select max(t2.col4)
from mytable t2
where t2.col1 = t.col1
);

Loop variable SQL query to append tables

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
...)

INSERT INTO using a query, and add a default value

I want run an INSERT INTO table SELECT... FROM...
The problem is that the table that I am inserting to has 5 columns, whereas the table I am selecting from has only 4. The 5th column needs to be set do a default value that I specify. How can I accomplish this? The query would be something like this (note: this is Oracle):
INSERT INTO five_column_table
SELECT * FROM four_column_table
--and a 5th column with a default value--;
Just add the default value to your select list.
INSERT INTO five_column_table
SELECT column_a, column_b, column_c, column_d, 'Default Value'
FROM four_column_table;
Just select the default value in your SELECT list. It's always a good idea to explicitly list out columns so I do that here even though it's not strictly necessary.
INSERT INTO five_column_table( col1, col2, col3, col4, col5 )
SELECT col1, col2, col3, col4, 'Some Default'
FROM four_column_table
If you really don't want to list out the columns
INSERT INTO five_column_table
SELECT fct.*, 'Some Default'
FROM four_column_table fct
Oracle supports a keyword DEFAULT for this purpose:
insert all
into five_column_table( col1, col2, col3, col4, col5 )
VALUES( col1, col2, col3, col4, DEFAULT)
SELECT col1, col2, col3, col4
FROM four_column_table;
But in your case I had to use multi-table insert. DEFAULT keyword can be used only in values clause.

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