![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and tbl_B and find 1)all rows in tbl_A that are different in tbl_B, 2)all rows in tbl_A that are not present in tbl_B and 3)all rows in tbl_B that are not present in tbl_A, and then just show those rows. |
|
Can this be done with a simple UNION? Perhaps this could produce a temp Table that can be dropped once the DTS package exists successfully. |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
The answer is to write 3 queries, 1 for each situation you describe and link the output using the UNION ALL statement Something like SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a JOIN table2 b on a.id = b.id WHERE a.col1 <> b.col1 UNION ALL SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a LEFT JOIN table2 b on a.id = b.id WHERE b.col4 IS NULL UNION ALL SELECT b.id, b.col1, b.col2, b.col3, a.col4 FROM table2 b LEFT JOIN table1 a on b.id = a.id WHERE a.col4 IS NULL |
|
Note that the col1, col2, col3 and col4 in each of the queries must be of the same datatypes or can be converted to the same datatypes e.g. VARCHAR(20) is the same as CHAR(5). The where clauses can be more specific and varied than I have shown but the important part is the test for IS NULL in the last 2 queries as this is the way to find rows in one table and that are not in another. Hope this helps |
#5
| |||
| |||
|
|
On 12 Jun, 16:24, undercups <d... (AT) woodace (DOT) co.uk> wrote: The answer is to write 3 queries, 1 for each situation you describe and link the output using the UNION ALL statement Something like SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a JOIN table2 b on a.id = b.id WHERE a.col1 <> b.col1 |
|
That's great! Thank you. I'll have a look into this. Do you have any ideas how I can add the above mentioned STATUS column after each returned row? eg. If row is different in tbl_B from tbl_A then add 'M' at the end of the row, as has Master data. something like.. ("col1.value","col2.value","col3.value","M") |
#6
| |||
| |||
|
|
The answer is to write 3 queries, 1 for each situation you describe and link the output using the UNION ALL statement Something like SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a JOIN table2 b on a.id = b.id WHERE a.col1 <> b.col1 UNION ALL (etc.) |
#7
| |||
| |||
|
|
undercups wrote: The answer is to write 3 queries, 1 for each situation you describe and link the output using the UNION ALL statement Something like SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a JOIN table2 b on a.id = b.id WHERE a.col1 <> b.col1 UNION ALL (etc.) The problem is that, if the tables don't have primary keys, then they may not have a column like 'id'. (Or maybe they do, and it just isn't defined as a primary key - in which case, why not?) |
#8
| |||||
| |||||
|
|
On 13 Jun, 06:25, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote: undercups wrote: The answer is to write 3 queries, 1 for each situation you describe and link the output using the UNION ALL statement Something like SELECT a.id, a.col1, a.col2, a.col3, b.col4 FROM table1 a JOIN table2 b on a.id = b.id WHERE a.col1 <> b.col1 UNION ALL (etc.) The problem is that, if the tables don't have primary keys, then they may not have a column like 'id'. (Or maybe they do, and it just isn't defined as a primary key - in which case, why not?) Well there are about 5 or 6 cols in each table and I need to check each one to see if anything has changed. There is 1 column in each that is kind of like the ID you are talking about though it is not set to Primary Key. |
|
Anyway, this is what I have for the 1st query to find Modified rows in TableMaster... SELECT a.id, a.col2, a.col3, a.col4, a.col5, b.id, b.col2, b.col3, b.col4, b.col5 'M' AS status FROM TableMaster a JOIN TableSlave b on a.id = b.id WHERE (a.col2 <> b.col2) OR (a.col3 <> b.col3) OR (a.col4 <> b.col4) OR (a.col5 <> b.col5) UNION ALL Now does the UNION ALL command at the end imply that another query will follow and that the results from this query and the other query be joined? |
|
Will the above view query return what I'm looking for? - rows in TableMaster that are different in TableSlave? so tha later I can update TableSlave with these new modified row. |
|
How could I continue and query Rows that are present in TableMaster but not in TableSlave? - Status 'A' (add) |
|
Finally, I would like to put a 3rd query in for TableMaster and TableGrandMaster, which tells me which rows are present in TableGrandMaster but missing in TableMaster - Status 'D' (delete) |
![]() |
| Thread Tools | |
| Display Modes | |
| |