home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 17,584 of 19,505   
   Erland Sommarskog to sheperson@googlemail.com   
   Re: Import data from another database -    
   22 Jul 09 22:34:53   
   
   dac5da5d   
   From: esquel@sommarskog.se   
      
   sheperson@googlemail.com (sheperson@googlemail.com) writes:   
   > I have two databases with the same structure. They were filled by   
   > different users. Now I want to merge the two databases. I tried the   
   > Import Wizard in SQL Server 2008, but it failed. It is because some of   
   > the primary keys in the two databases are equal.   
   > I want to import only the non-duplicate rows from the other database,   
   > but I don't know how to do it. I tried writing a program in C# and it   
   > actually does the job for me, but it is terribly slow.   
   > Is it possible to do it in SQL Server itself?   
      
   INSERT tbl (keycol1, keycol2, ..., col1, col2, ...)   
      SELECT b.keycol1, b.keycol2, ..., b.col1, b.col2, ...   
      FROM   otherdb..tbl b   
      WHERE  NOT EXISTS (SELECT *   
                         FROM   tbl a   
                         WHERE  a.keycol1 = b.keycol1   
                           AND  a.keycol2 = b.keycol2   
                           ...)   
      
      
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Links for SQL Server Books Online:   
   SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx   
   SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx   
   SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca