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:




  1. Get the first row.

  2. FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),

  3. If ID_FT == ID_ST then NAME_FT = NAME_ST,

  4. Get the next row,

  5. 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?










share|improve this question






















  • 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















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:




  1. Get the first row.

  2. FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),

  3. If ID_FT == ID_ST then NAME_FT = NAME_ST,

  4. Get the next row,

  5. 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?










share|improve this question






















  • 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













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:




  1. Get the first row.

  2. FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),

  3. If ID_FT == ID_ST then NAME_FT = NAME_ST,

  4. Get the next row,

  5. 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?










share|improve this question













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:




  1. Get the first row.

  2. FIRST_TABLE(ID_FT, NAME_FT) and SECOND_TABLE(ID_ST, NAME_ST),

  3. If ID_FT == ID_ST then NAME_FT = NAME_ST,

  4. Get the next row,

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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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





share|improve this answer




























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





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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





      share|improve this answer

























        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





        share|improve this answer























          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 20:12









          Oiale

          22617




          22617
























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





              share|improve this answer

























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





                share|improve this answer























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





                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 17:59









                  Gordon Linoff

                  743k32285390




                  743k32285390






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Full-time equivalent

                      Bicuculline

                      さくらももこ