Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:34:28+00:00 2026-06-17T23:34:28+00:00

I have 2 tables in mssql server.I can perform scd through custom insert/update/delete and

  • 0

I have 2 tables in mssql server.I can perform scd through custom insert/update/delete and also through Merge statement.

  • Awesome Merge

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:34:30+00:00Added an answer on June 17, 2026 at 11:34 pm

    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:

    • How do you know which SCD type? (Okay, could be another parameter, but…)
    • How do you know which column should be historicized and which should be overwritten?
    • How do you determine which column is the business key, the surrogate key, the expiration column and so on?
    • To specify the columns in an update statement you must write dynamic sql, which is possible, but the above point comes into play

    Not a reason why it’s not possible but also consider: For a proper UPSERT one usually works with temporary tables, the MERGE statement sucks for SCDs except in special cases. That is because you can’t use a MERGE statement together with an INSERT/UPDATE and you would have to disable foreign keys for that, since an UPDATE is implemented as DELETE 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:

    IF EXISTS (
    SELECT * FROM sys.objects
    WHERE name = 'tmpDimSource')
    DROP TABLE tmpDimSource;
    SELECT
    *
    INTO tmpDimSource
    FROM
    (
    SELECT whatever
    FROM yourTable
    );
    

    Step 2:

    IF EXISTS (
    SELECT * FROM sys.objects
    WHERE name = 'tmpDimYourDimensionName')
    DROP TABLE tmpDimYourDimensionName;
    
    SELECT * INTO tmpDimYourDimensionName FROM D_yourDimensionName WHERE 1 = 0;
    INSERT INTO tmpDimYourDimensionName 
    (
    sid, /*a surrogate id column*/
    theColumnsYouNeedInYourDimension,
    validFrom
    )
    SELECT 
    ISNULL(d.sid, 0),
    ds.theColumnsYouNeedInYourDimension,
    DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) /*the current date*/
    FROM
    tmpDimSource ds 
    LEFT JOIN D_yourDimensionName d ON ds.whateverId = c.whateverId
    ;
    

    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:

    UPDATE D_yourDimensionName SET 
    validTo = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 1, 0) /*yesterday*/
    FROM 
    D_yourDimensionName d
    INNER JOIN tmpDimYourDimensionName t ON d.sid = t.sid
    WHERE t.sid <> 0 AND
    (
    d.theColumnWhichHasChangedAndIsImportant <> t.theColumnWhichHasChangedAndIsImportant OR
    d.anotherColumn <> t.anotherColumn 
    )
    ;
    

    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 UPDATE to overwrite any other column with the new value if needed.

    Step 4:

    INSERT INTO D_yourDimensionName 
    SELECT * FROM tmpDimYourDimensionName 
    WHERE sid = 0;
    

    And that’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in MSSQL Server 2012 and would like to combine them
I will have multiple tables used by different projects on the same mySql server.
I came around some weird thing in MS SQL server. I have two tables
I have a question: I have two MSSQL tables, items and states, that are
Say I have a table like this in my MsSql server 2005 server Apples
I have two tables (MS SQL Server 2005) with an existing application (no DB
I have an MSSQL Server 2008 database DBLive that link to three different external
I use Visual Studio 2008, C#, MS SQL Server 2005. I have 2 tables
I have been wondering about temp tables in sp's and how all that can
I'm attempting to initialize sonar with mssql server database, but I can't. With Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.