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. Difference between two tables

Difference between two tables

Scheduled Pinned Locked Moved Database
databasequestion
4 Posts 2 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.
  • N Offline
    N Offline
    niceguyeddie1999
    wrote on last edited by
    #1

    Hi, I have two identical tables in 2 seperate databases. 1 is the "central" DB and its contents can change. Basically I want to write a poece of code to run periodically to ensure the contents of table 2 are up to date with that of table 1. Id imagine this is a relativaly common task so I was wondering are there any smart ways of doing it or any existing code knocking around? Thanks in advance,

    C 1 Reply Last reply
    0
    • N niceguyeddie1999

      Hi, I have two identical tables in 2 seperate databases. 1 is the "central" DB and its contents can change. Basically I want to write a poece of code to run periodically to ensure the contents of table 2 are up to date with that of table 1. Id imagine this is a relativaly common task so I was wondering are there any smart ways of doing it or any existing code knocking around? Thanks in advance,

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You could try something like this:

      SELECT a.pk, b.pk
      FROM FirstDatabase.dbo.TableName AS a
      FULL OUTER JOIN SecondDatabase.dbo.TableName AS b WHERE a.pk = b.pk
      WHERE a.pk IS NULL
      OR b.pk IS NULL

      pk = primary key, if you have a compound key then you will need all the columns that make up the primary key. The results of the query should (I haven't tested it) return any rows that exist in one database, but don't in the other. If you want to return all rows that have differences then you might want to add to the WHERE clause:

      OR a.column1 <> b.column1
      OR a.column2 <> b.column2

      ...and so on for each of the columns. ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

      N 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You could try something like this:

        SELECT a.pk, b.pk
        FROM FirstDatabase.dbo.TableName AS a
        FULL OUTER JOIN SecondDatabase.dbo.TableName AS b WHERE a.pk = b.pk
        WHERE a.pk IS NULL
        OR b.pk IS NULL

        pk = primary key, if you have a compound key then you will need all the columns that make up the primary key. The results of the query should (I haven't tested it) return any rows that exist in one database, but don't in the other. If you want to return all rows that have differences then you might want to add to the WHERE clause:

        OR a.column1 <> b.column1
        OR a.column2 <> b.column2

        ...and so on for each of the columns. ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

        N Offline
        N Offline
        niceguyeddie1999
        wrote on last edited by
        #3

        Sorry I should have mentioned the two DB's are actually running on 2 different servers

        C 1 Reply Last reply
        0
        • N niceguyeddie1999

          Sorry I should have mentioned the two DB's are actually running on 2 different servers

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          That's okay. The naming convention extends to servers. Just add in the server name like this: ServerName.DatabaseName.SchemaName.TableName SchemaName is dbo, unless you've set it up otherwise. You will also have to link the two servers together. You might find this useful: MSDN: Configuring Linked Servers[^] ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

          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