OK, here's what I'm trying to do:
I'm writing a script which will allow the contents of ANY two tables to be compared (assuming they both have identical column names & datatypes).
The aim is to examine the contents of Col_1 from Table_1 and check to see if any of the contained values differ from those in Col_1 of Table_2. If there is a differing value in any row, I have to copy that entire row to a separate results table. The process is repeated for Columns_2 to Column_x until it reaches the last column.
For example , at a simple level, assume the two tables chosen each have three columns and no unique identifier column.
Table_1:
row 1: John, Smith, Male
row 2: Fred, Smith, Male
row 3: Charlie, Smith, Male
Table_2:
row 1: John, Smith, Male
row 2: Peter, Smith, Male
row 3: Charlie, Smith, Male
I'm looking for the ability to loop through each column of Table_1, starting at col_1, identifying that it contains John, Fred and Charlie. I then look at Col_1 of Table_2 and discover that my dynamically-built "NOT IN('John', 'Fred', 'Charlie')" clause returns 'Peter'. I then have to take the entire contents of that row where 'Peter' appears and copy it's contents to another results table.
To do this, I decided that the best way would be to use two cursors (one nested inside the other) and to dynamically build the string which both creates the WHERE clause for the nested cursor and (because the tables may have an as yet undetermined number of columns) declare each cursor variable dynamically as the cursor itself is created. This is where my idea has fallen down.
I initially looked at the possibilities of using the EXCEPT and INTERSECT keywords with simple queries but these won't do the job I need.
Thanks.