Updating “field by field”: copying data from one table to another
up vote
1
down vote
favorite
I have two tables, which are basically the same except the name: one for actual data second one for backup. And what I want to do is to update fields in first one using the content from the other one - checking field by field based on ID.
Let's say it should looks like this:
- Get the first row.
- FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),
- If ID_FT == ID_ST then NAME_FT = NAME_ST,
- Get the next row,
- Go to point 3 and loop until end of the FIRST_TABLE.
What queries should I use? How iterating through the table in ORACLE/SQL looks like?
sql oracle
add a comment |
up vote
1
down vote
favorite
I have two tables, which are basically the same except the name: one for actual data second one for backup. And what I want to do is to update fields in first one using the content from the other one - checking field by field based on ID.
Let's say it should looks like this:
- Get the first row.
- FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),
- If ID_FT == ID_ST then NAME_FT = NAME_ST,
- Get the next row,
- Go to point 3 and loop until end of the FIRST_TABLE.
What queries should I use? How iterating through the table in ORACLE/SQL looks like?
sql oracle
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two tables, which are basically the same except the name: one for actual data second one for backup. And what I want to do is to update fields in first one using the content from the other one - checking field by field based on ID.
Let's say it should looks like this:
- Get the first row.
- FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),
- If ID_FT == ID_ST then NAME_FT = NAME_ST,
- Get the next row,
- Go to point 3 and loop until end of the FIRST_TABLE.
What queries should I use? How iterating through the table in ORACLE/SQL looks like?
sql oracle
I have two tables, which are basically the same except the name: one for actual data second one for backup. And what I want to do is to update fields in first one using the content from the other one - checking field by field based on ID.
Let's say it should looks like this:
- Get the first row.
- FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),
- If ID_FT == ID_ST then NAME_FT = NAME_ST,
- Get the next row,
- Go to point 3 and loop until end of the FIRST_TABLE.
What queries should I use? How iterating through the table in ORACLE/SQL looks like?
sql oracle
sql oracle
asked Nov 10 at 17:50
Jacek Krzyżanowski
675
675
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55
add a comment |
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You can try to use MERGE statement.
Test data:
SQL> create table FIRST_TABLE (
2 ID_FT number(20),
3 NAME_FT varchar2(200));
4 create table SECOND_TABLE (
5 ID_ST number(20),
6 NAME_ST varchar2(200));
7 insert into FIRST_TABLE (ID_FT,NAME_FT) values (1,null);
8 insert into FIRST_TABLE (ID_FT,NAME_FT) values (2,null);
9 insert into SECOND_TABLE (ID_ST,NAME_ST) values (1,'ST1');
10 insert into SECOND_TABLE (ID_ST,NAME_ST) values (2,'ST2');
11 insert into SECOND_TABLE (ID_ST,NAME_ST) values (3,'ST3');
12 commit;
Query:
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st;
5 commit;
6 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
Also, if you will use MERGE
, you can insert data in first_table if it's missing.
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st
5 when not matched then insert (t1.id_ft,t1.name_ft) values (t2.id_st,t2.name_st);
6 commit;
7 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
3 ST3
add a comment |
up vote
0
down vote
I think you just want an update with a correlated subquery:
update first_table ft
set name_ft = (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST)
where exists (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can try to use MERGE statement.
Test data:
SQL> create table FIRST_TABLE (
2 ID_FT number(20),
3 NAME_FT varchar2(200));
4 create table SECOND_TABLE (
5 ID_ST number(20),
6 NAME_ST varchar2(200));
7 insert into FIRST_TABLE (ID_FT,NAME_FT) values (1,null);
8 insert into FIRST_TABLE (ID_FT,NAME_FT) values (2,null);
9 insert into SECOND_TABLE (ID_ST,NAME_ST) values (1,'ST1');
10 insert into SECOND_TABLE (ID_ST,NAME_ST) values (2,'ST2');
11 insert into SECOND_TABLE (ID_ST,NAME_ST) values (3,'ST3');
12 commit;
Query:
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st;
5 commit;
6 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
Also, if you will use MERGE
, you can insert data in first_table if it's missing.
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st
5 when not matched then insert (t1.id_ft,t1.name_ft) values (t2.id_st,t2.name_st);
6 commit;
7 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
3 ST3
add a comment |
up vote
2
down vote
You can try to use MERGE statement.
Test data:
SQL> create table FIRST_TABLE (
2 ID_FT number(20),
3 NAME_FT varchar2(200));
4 create table SECOND_TABLE (
5 ID_ST number(20),
6 NAME_ST varchar2(200));
7 insert into FIRST_TABLE (ID_FT,NAME_FT) values (1,null);
8 insert into FIRST_TABLE (ID_FT,NAME_FT) values (2,null);
9 insert into SECOND_TABLE (ID_ST,NAME_ST) values (1,'ST1');
10 insert into SECOND_TABLE (ID_ST,NAME_ST) values (2,'ST2');
11 insert into SECOND_TABLE (ID_ST,NAME_ST) values (3,'ST3');
12 commit;
Query:
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st;
5 commit;
6 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
Also, if you will use MERGE
, you can insert data in first_table if it's missing.
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st
5 when not matched then insert (t1.id_ft,t1.name_ft) values (t2.id_st,t2.name_st);
6 commit;
7 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
3 ST3
add a comment |
up vote
2
down vote
up vote
2
down vote
You can try to use MERGE statement.
Test data:
SQL> create table FIRST_TABLE (
2 ID_FT number(20),
3 NAME_FT varchar2(200));
4 create table SECOND_TABLE (
5 ID_ST number(20),
6 NAME_ST varchar2(200));
7 insert into FIRST_TABLE (ID_FT,NAME_FT) values (1,null);
8 insert into FIRST_TABLE (ID_FT,NAME_FT) values (2,null);
9 insert into SECOND_TABLE (ID_ST,NAME_ST) values (1,'ST1');
10 insert into SECOND_TABLE (ID_ST,NAME_ST) values (2,'ST2');
11 insert into SECOND_TABLE (ID_ST,NAME_ST) values (3,'ST3');
12 commit;
Query:
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st;
5 commit;
6 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
Also, if you will use MERGE
, you can insert data in first_table if it's missing.
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st
5 when not matched then insert (t1.id_ft,t1.name_ft) values (t2.id_st,t2.name_st);
6 commit;
7 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
3 ST3
You can try to use MERGE statement.
Test data:
SQL> create table FIRST_TABLE (
2 ID_FT number(20),
3 NAME_FT varchar2(200));
4 create table SECOND_TABLE (
5 ID_ST number(20),
6 NAME_ST varchar2(200));
7 insert into FIRST_TABLE (ID_FT,NAME_FT) values (1,null);
8 insert into FIRST_TABLE (ID_FT,NAME_FT) values (2,null);
9 insert into SECOND_TABLE (ID_ST,NAME_ST) values (1,'ST1');
10 insert into SECOND_TABLE (ID_ST,NAME_ST) values (2,'ST2');
11 insert into SECOND_TABLE (ID_ST,NAME_ST) values (3,'ST3');
12 commit;
Query:
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st;
5 commit;
6 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
Also, if you will use MERGE
, you can insert data in first_table if it's missing.
SQL> merge into first_table t1
2 using second_table t2
3 on (t1.id_ft = t2.id_st)
4 when matched then update set t1.name_ft = t2. name_st
5 when not matched then insert (t1.id_ft,t1.name_ft) values (t2.id_st,t2.name_st);
6 commit;
7 select * from first_table;
ID_FT NAME_FT
---------- ----------
1 ST1
2 ST2
3 ST3
answered Nov 10 at 20:12
Oiale
22617
22617
add a comment |
add a comment |
up vote
0
down vote
I think you just want an update with a correlated subquery:
update first_table ft
set name_ft = (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST)
where exists (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST);
add a comment |
up vote
0
down vote
I think you just want an update with a correlated subquery:
update first_table ft
set name_ft = (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST)
where exists (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST);
add a comment |
up vote
0
down vote
up vote
0
down vote
I think you just want an update with a correlated subquery:
update first_table ft
set name_ft = (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST)
where exists (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST);
I think you just want an update with a correlated subquery:
update first_table ft
set name_ft = (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST)
where exists (select t2.name_st from second_table st where ft.ID_FT = st.ID_ST);
answered Nov 10 at 17:59
Gordon Linoff
743k32285390
743k32285390
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241795%2fupdating-field-by-field-copying-data-from-one-table-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Would it make sense to use a database backup tool to backup the table instead?
– Schwern
Nov 10 at 17:54
Well - the system is a little bit complicated and it have to contain additional backup table because it allows avoiding some problems with it.
– Jacek Krzyżanowski
Nov 10 at 17:55