sql server 2005
I'm not sure the best way to go about this, your suggestions are appreciated.
I need to get matching records between 2 tables and insert the results into a 3rd table. These 2 tables are not alike except for a couple of fileds.
I have a select statement that does a CROSS JOIN on 2 tables then I need to insert the results into a 3rd table. The primaryKeys of the first 2 tables form the composite primary key on the 3rd table. Before inserting into the 3rd table I need to remove any records in the select statement that already exist in the 3rd table.
i.e.
Code:INSERT table3 SELECT key1, key2 FROM table1 CROSS JOIN table2 WHERE table1.xx = 'true' AND table2.xx = 'false' AND table1.yy > table2.yy AND NOT EXISTS(SELECT key1 FROM table3 WHERE key1 = table1.key1AND key2 = table2.key2)
Now this is a heafty query with the cross join and exists. Does the exists statement execute every time or only if the preceeding where statements succeed.
OR is there a better way to write this type of query.




Reply With Quote