I have two tables which have the exact same structure. Both tables can store the same data with different primary keys (autoincremented integers). Therefore, there is a third table which lists which two primary keys list the same data. However, there also exist rows which don’t exist in the other. Therefore, a simple join won’t work since you will have two rows with the same primary key but different data. Therefore, is there a way of reassigning primary keys to unused values in the view?
Table1
ID name 1 Adam 2 Mark 3 David 4 Jeremy
Table2
ID name 1 Jessica 2 Jeremy 3 David 4 Mark
Table3
T1ID T2ID 2 4 3 3 4 2
I am looking for a result table like the following:
Result
ID name 1 Adam 2 Mark 3 David 4 Jeremy 5 Jessica
The real heart of the question is how i can assign the temporary fake id of 5 to Jessica and not just some random number. The rule I want for the ids is that if the row exists in the first table, then use its own id. Otherwise, use the next id that an insert statement would have generated (the column is on autoincrement).
Answer to edited question
The MAX(id) is used to “predict” the next identity that would occur if you merged the data from the 2nd table into the first. If Table3.T2ID exists at all, it means that it is already included in table1.
Using the test data below
Answer to original question below
So the 3rd table is the one you want to build (a view instead of a table)?
The data will contain a unique
newidvalue for each record, whether from first or second table. Changepk_idto your primary key column name.