Update and insert to one table from another - sql

I have two tables:
table1: (ID, Code, Name)
table2: (ID, Code, Name)
with same columns
I want to to insert data from table1 to table2 or update columns if that exists in table2 (table1.ID = table2.ID)
What is the simple way to do this?
WITH OUT MERGE

Merge table2 as target
using table1 as source
on
target.id=source.id
When matched
Then
update
set target.id=source.id,
target.name=source.name
When not matched by Target Then
INSERT (id, name) VALUES (id, name);
There are some issues with Merge statement,so it should be used with caution..
Further i recommend ,using merge as two seperate DML statements like below..
insert into table2
select * from table1 t1 where not exists (select 1 from table2 t2 where t2.id=t1.id)
update t2
set
t2.id=t1.id,
t2.name=t1.name
from
table1 t1
join
table2 t2
on t1.id=t2.id
Reasons being stated by Paul White here in his detailed answer..

MERGE table2 t2
USING table1 t1
ON t1.ID = t2.ID
WHEN MATCHED THEN
UPDATE
SET t2.Code = t1.Code, t2.Name = t1.Name
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID, Name, Code)
VALUES (t1.ID, t1.Name, t1.Code);

Assuming the ID column is unique and should not be set, it seems you could do it in two SQL Statements.
/* UPDATE the rows in TABLE2 */
UPDATE TABLE2
SET NAME = (SELECT NAME FROM TABLE1 WHERE TABLE1.CODE = TABLE2.CODE)
WHERE CODE IN (SELECT CODE FROM TABLE1)
/* INSERT the rows that are missing */
INSERT INTO TABLE2
(CODE, NAME)
(
SELECT CODE, NAME
FROM TABLE1
WHERE CODE NOT IN (SELECT CODE FROM TABLE2)
)

get all rows that are in table1 but not in table2
insert into table2(id, code, name)(
SELECT table1.*
FROM table1
LEFT JOIN table2 ON (table1.id = table2.id)
WHERE table2.C IS NULL
)
update table2
update table2 set name = (select name from table1 where table1.code = table2.code and table1.id = table2.id)
It might be worth looking at triggers on update and insert if you'd like to have this done manually

Inserting Date into target table once date is updated in source table
Here there is a working example:
create table Table1(id int,name varchar(100));
create table Table2(id int,name varchar(100));
create trigger Table1Trigger after insert on Table1 for each row begin
insert into Table2(id, name) values (new.id, new.name);
end;
Use the following queries to validate the results
insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol');

Here I am writing a script that use full when you want to update table2 from table1.
Update table2 set table2.code = table1.code, table2.name=table1.name from table1 where table2.id=table1.id
And if you want to insert then use this script.
Insert into table2 (id,code,name) select id,code,name from table1
If in table2 id is not auto increment.
Otherwise not insert the value of id column in table2.

Related

SQL how to insert from another table based on same row values?

I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name

How do I only import unique records into a table from another table?

I am trying to update a table in SQL Server. Table is called table1 with columns(code, desc). I want to update table1 with unique records from table2(code, desc). table2 is a copy of table1 but it contains records that are not present in table1.
EDIT 1: table1 consists of all the records in our ERP when exported in July, table2 consists of all the records in our ERP when exported in November (including records added after July which are the records that I want to add to table1)
To update table1.desc with values from matching rows in table2 simply do:
update t1 set
t1.desc = t2.desc
from table1 t1
join table2 t2 on t2.code = t1.code;
If however you want to insert rows into table1 that only exist in table2 (it's not exactly clear if that's the case) you can use not exists
insert into table1 (code, desc)
select code, desc
from table2 t2
where not exists (select * from table1 t1 where t1.code = t2.code);
Sounds like you want an INSERT
Something like this should work:
INSERT INTO table1 (code, desc)
SELECT t2.code, t2.desc
FROM table2 t2
LEFT JOIN table1 t1 on t2.code = t1.code and t1.desc = t2.desc
WHERE t1.code is null --Ignore records that already exist in table1
... Adjust join clause accordingly.

Oracle INSERT statement with selects on different tables

I have two tables table1 and table2. Those tables have unique name and id columns.
I also have a relation / join table, table1_table2 which has the straight-forward colums table1_id and table2_id.
What I want to do is to insert a new relation into table1_table2 knowing the names of the elements in table1 and table2 I want to create a relation for. But I need to get their ids to insert them into table_table2.
What I want is something like that:
insert into table1_table2 values ((select id from table1 where name = 'some_name'), (select id from table2 where name = 'another_name'))
I also tried using
insert into table1_table2 values ((select id from (select id from table1 where name = 'some_name') where rownum=1), (select id from (select id from table2 where name = 'another_name') where rownum=1))
which also didn't work.
I understand I can first extract the ids if necessary but I'd prefer it to be in one statement.
Edit: I've also tried
insert into table1_table2 values (select t1.id, t2.id from table1 t1, table2 t2 where t1.name = 'some_name' and t2.name = 'another_name')
which also didn't work
Example data:
table1
id name
1 foo
2 bar
table2
id name
1 some
2 data
table1_table2
table1.id table2.id
1 1
and now I want to insert
table1.id table2.id
2 2
into table1_table2, but I do only know that the entry in table1 has the name bar and the entry in table2 has the name data.
This should work:
INSERT INTO table1_table2 (table1_id, table2_id)
VALUES ( (SELECT id FROM table1 WHERE name = 'some_name'),
(SELECT id FROM table2 WHERE name = 'another_name')
);
However, I would write it as:
INSERT INTO table1_table2 (table1_id, table2_id)
SELECT t1.id, t2.id
FROM table1 t1 JOIN
table2 t2
ON t1.name = 'some_name' AND
t2.name = 'another_name';
Note in this case if there is no match in either table, then no row is inserted at all. Using VALUES, a NULL values would be inserted.

ORACLE SQL:More than one WHEN MATCHED condition INSIDE MERGE STATEMENT

I want to use a merge statement in pl/sql and the target table has to be updated on a particular condition. I know we can use multiple when matched condition in sql server with and operator, but i am not sure about Oracle. I am looking for that kind of feature in oracle.
create table table1 (id number, text varchar2(20));
create table table2 (id number, text varchar2(20), text2 varchar2(20));
insert into table1 values (1,'');
insert into table1 values (2,'');
insert into table1 values (3,'');
insert into table2 values (1,'a','b');
insert into table2 values (2,'a','b');
insert into table2 values (3,'a','b');
insert into table2 values (4,'a','b');
I understand you want obtain something like code below with commented line
merge into table1 t1
using table2 t2
on (t1.id = t2.id)
when matched then update set
t1.text = t2.text where t1.id = 1
--t1.text = t2.text2 where t1.id = 2
when not matched then insert values (t2.id, t2.text);
I would do that like:
merge into table1 t1
using (select id, case id when 1 then text when 2 then text2 else null end as tt, text from table2) t2
on (t1.id = t2.id)
when matched then update set
t1.text = t2.tt where t2.tt is not null
--t1.text = t2.text2 where t1.id = 2
when not matched then insert values (t2.id, t2.text);
select * from table1;

insert into table1 from table2 with condition

I have two tables, and I want to insert data in one of the new column (Orders) of table1 from table2. Any help please, below code is not working
Insert into Table1 (Orders)
Select (orders) from table2,
Where table1.id=table2.id
try this:
update t1
set t1.orders=t2.orders
from Table1 t1
inner join table2 t2
on t1.id=t2.id
UPDATE Table1
SET Orders = (
SELECT Orders
FROM Table2
WHERE table1.Id = table2.Id
)