I have 2 tables in mssql server.I can perform scd through custom insert/update/delete and also through Merge statement.
I want to know that is there any generic procedure that could server the purpose. we just pass it 2 tables and it should porform the SCD. any option in SQL server 2008?
Thanks
No, there isn’t and there can’t be a generic one suitable for no matter what tables you pass to it. For several reasons:
Not a reason why it’s not possible but also consider: For a proper
UPSERTone usually works with temporary tables, theMERGEstatement sucks for SCDs except in special cases. That is because you can’t use aMERGEstatement together with anINSERT/UPDATEand you would have to disable foreign keys for that, since anUPDATEis implemented asDELETE THEN INSERT(or something like that, don’t remember clearly, but I had those problems when I tried).I prefer doing it this way (SCD type 2 and SQL Server that is):
Step 1:
Step 2:
The
ISNULL(d.sid, 0)in step 2 is important. It returns the surrogate id of your dimension, if an entry already exists, otherwise 0.Step 3:
In Step 3 you mark the existing entry as not valid anymore and keep a history of it. The valid entry you get with
WHERE validTo IS NULL.You can also add another
UPDATEto overwrite any other column with the new value if needed.Step 4:
And that’s it.