title: What's going wrong with this sql query?
body: <p>I have two tables <code>BankMaster</code> and <code>#tmp_BankMaster</code>. The <code>#tmp_BankMaster</code> table is inherited from <code>BankMaster</code> table. So, data structures are same. Even some data also exist in <code>#tmp_BankMaster</code> table is the same as <code>BankMaster</code>. Now I want to insert all non-existing data from <code>#tmp_BankMaster</code> into <code>BankMaster</code>. I have created a query. But, don't know how it gives an error primary key constraint: "Cannot insert duplicate key". </p>
  
  <pre><code>INSERT INTO BankMaster 
     SELECT * 
     FROM #tmp_BankMaster 
     WHERE  
         BankID NOT IN (SELECT BankID FROM BankMaster 
                        WHERE BankMaster.BankID = #tmp_BankMaster.BankID 
                          AND BankMaster.CompanyID = #tmp_BankMaster.CompanyID )
  </code></pre>
  
  <p>I am using SQL Server 2005 and primary key is created on the columns <code>CompanyID</code> and <code>BankID</code>. It is a composite key.</p>