Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Copy table 1 to table 2 for missing data only

Copy table 1 to table 2 for missing data only

Scheduled Pinned Locked Moved Database
help
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MAW30
    wrote on last edited by
    #1

    I have two tables that are identical, 1 table has old data and the other new data. I want to copy the old to the new where the records are either blank or null is there an easy way I can do this. Any help is appreciated. Thanks in advance, Michael

    B P D 3 Replies Last reply
    0
    • M MAW30

      I have two tables that are identical, 1 table has old data and the other new data. I want to copy the old to the new where the records are either blank or null is there an easy way I can do this. Any help is appreciated. Thanks in advance, Michael

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Which column is unique on both tables? Syntax is like this:

      Insert into table2 (col1,col2,col3)
      select col1,col2,col3 from table1

      In this select statement you have to include where clause with unique column in table1 and tale2


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

      1 Reply Last reply
      0
      • M MAW30

        I have two tables that are identical, 1 table has old data and the other new data. I want to copy the old to the new where the records are either blank or null is there an easy way I can do this. Any help is appreciated. Thanks in advance, Michael

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Are they in the same database? Are they on the same server? Are they the same brand of database? When I had to copy data from an Ingres database on a Unix system to an SQL Server database on Windows there were times I had to simply throw the records at the database one by one and catch-log-and-ignore any duplicate Exceptions encountered. Simple but effective, and quicker than trying to test each record first.

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          Are they in the same database? Are they on the same server? Are they the same brand of database? When I had to copy data from an Ingres database on a Unix system to an SQL Server database on Windows there were times I had to simply throw the records at the database one by one and catch-log-and-ignore any duplicate Exceptions encountered. Simple but effective, and quicker than trying to test each record first.

          M Offline
          M Offline
          MAW30
          wrote on last edited by
          #4

          They are in the same DB on the same server. I copy all the old records to a similar table Delete the orig table's records Start with a blank table, this is a starting point to build other info, I do not want many of the records from the old included which I do not know until I start new. Add new data Then I want to insert old data where new data is left blank and do not wish to overwrite new data. The previous response said the following: Insert into table2 (col1,col2,col3) select col1,col2,col3 from table1 I am thinking if I used the above then added Where col1 IS LIKE '' or col1 IS NULL but I would have to do this for every column, I was hoping there would be a quick easy way Michael

          J 1 Reply Last reply
          0
          • M MAW30

            They are in the same DB on the same server. I copy all the old records to a similar table Delete the orig table's records Start with a blank table, this is a starting point to build other info, I do not want many of the records from the old included which I do not know until I start new. Add new data Then I want to insert old data where new data is left blank and do not wish to overwrite new data. The previous response said the following: Insert into table2 (col1,col2,col3) select col1,col2,col3 from table1 I am thinking if I used the above then added Where col1 IS LIKE '' or col1 IS NULL but I would have to do this for every column, I was hoping there would be a quick easy way Michael

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Might help to know which database vendor you are referring to. Easy is relative. You can do an update statement with a 'case' for each column.

            1 Reply Last reply
            0
            • M MAW30

              I have two tables that are identical, 1 table has old data and the other new data. I want to copy the old to the new where the records are either blank or null is there an easy way I can do this. Any help is appreciated. Thanks in advance, Michael

              D Offline
              D Offline
              dasblinkenlight
              wrote on last edited by
              #6

              SQL Server supports a rather convenient MERGE[^] command that lets you specify criteria of two records being 'identical', actions to take on finding a match, and actions to take on finding missing records. The documentation at the link supplies several large examples, so you should be able to construct your statement relatively easily.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups