Merge won't work (I think) because the target table has one additional column and I need to not delete data that isn't in the source table
So I have about 20 different identical databases that I pull data from. (This is a vendor system, I did not set that up) I iterate through each one and collect everything out of it into a single DB with an additional column to note the source DB.
Right now it's super messy. Truncate the table in the target DB, then iterate through each source DB to refill. Takes a long time, and we're repeating the load every time it runs. I'd rather update existing rows and insert new ones.
So far this has sped it up exponentially on the tables I've written out. However, I'm running into a snag. This might be a silly thing for me to get hung up on, I don't know, but I haven't gotten it yet. Maybe it's just because it's late in the day, I don't know.
The table in question has 2 primary keys: which we'll call C and S
my update works fine as
update t set t.field1 = src.field1 ,t.field2 = src.field2 ,t.etc = source.etc from target t inner join source src on t.C = src.C and t.S = src.S and t.db = @db
But I need the select equivalent of that. Neither C nor S are strictly unique. They're a unique pair - so you can't have C=10 S=10 in multiple rows, but you could have one row be C=10 S=11, another be C=10 S=12, and another be C=11, S=11
My goal is something along the lines of:
insert into target (columns) select columns from source where (paired keys) not in (select (paired keys) from target)
How can I value pair these keys, I guess?