How to insert from Table 1 to Table 2 only if record doesn't exist in Table 2.

  • I'm inserting from TempAccrual to VacationAccrual . It works nicely, however if I run this script again it will insert the same values again in VacationAccrual. How do I block that? IF there is a small change in one of the column in TempAccrual then allow insert. Here is my query

    INSERT INTO vacationaccrual

    (empno,

    accrued_vacation,

    accrued_sick_effective_date,

    accrued_sick,

    import_date)

    SELECT empno,

    Max(CASE

    WHEN = 'S' THEN [hrs]

    END),

    [effective date],

    Max(CASE

    WHEN = 'T' THEN [hrs]

    END),

    import_date

    FROM tempaccrual T

    GROUP BY empno,

    [effective date],

    [import_date]

  • INSERT INTO vacationaccrual

    (empno,

    accrued_vacation,

    accrued_sick_effective_date,

    accrued_sick,

    import_date)

    SELECT empno,

    AccruedVacation,

    [effective date],

    AccruedSick,

    import_date

    FROM (SELECT empno,

    MAX(CASE

    WHEN code = 'S' THEN hrs

    END) AS AccruedVacation,

    [effective date] AS accrued_sick_effective_date,

    MAX(CASE

    WHEN code = 'T' THEN hrs

    END) AS AccruedSick,

    import_date

    FROM tempaccrual T

    GROUP BY empno,

    [effective date],

    import_date

    ) s

    WHERE NOT EXISTS ( SELECT 1

    FROM vacationaccrual v

    WHERE v.empno = s.empno AND v.accrued_vacation = s.accrued_vacation AND v.accrued_sick_effective_date = s.accrued_sick_effective_date AND v.accrued_sick = s.accrued_sick AND v.import_date = s.import_date )

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • shezi (7/24/2015)


    I'm inserting from TempAccrual to VacationAccrual . It works nicely, however if I run this script again it will insert the same values again in VacationAccrual. How do I block that? IF there is a small change in one of the column in TempAccrual then allow insert. Here is my query

    INSERT INTO vacationaccrual

    (empno,

    accrued_vacation,

    accrued_sick_effective_date,

    accrued_sick,

    import_date)

    SELECT empno,

    Max(CASE

    WHEN = 'S' THEN [hrs]

    END),

    [effective date],

    Max(CASE

    WHEN = 'T' THEN [hrs]

    END),

    import_date

    FROM tempaccrual T

    GROUP BY empno,

    [effective date],

    [import_date]

    You need to do a LEFT JOIN back to the destination table, and then check for a non-nullable column being null. Like this:

    WITH T AS

    (

    SELECT empno,

    Max(CASE

    WHEN = 'S' THEN [hrs]

    END) AS accrued_vacation,

    [effective date],

    Max(CASE

    WHEN = 'T' THEN [hrs]

    END) AS accrued_sick,

    import_date

    FROM tempaccrual T

    GROUP BY empno,

    [effective date],

    [import_date]

    )

    INSERT INTO vacationaccrual

    (empno,

    accrued_vacation,

    accrued_sick_effective_date,

    accrued_sick,

    import_date)

    SELECT T.empno,

    T.accrued_vacation,

    T.effective_date,

    T.accrued_sick,

    T.import_date

    FROM T

    LEFT JOIN vacationaccrual T2

    ON T.empno = T2.empno

    AND T.accrued_vacation = T2.accrued_vacation

    AND T.effective_date = T2.accrued_sick_effective_date

    AND T.accrued_sick = T2.accrued_sick

    AND T.import_date = T2.import_date

    WHERE T2.empno IS NULL;

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • GilaMonster (7/24/2015)


    INSERT INTO vacationaccrual

    (empno,

    accrued_vacation,

    accrued_sick_effective_date,

    accrued_sick,

    import_date)

    SELECT empno,

    AccruedVacation,

    [effective date],

    AccruedSick,

    import_date

    FROM (SELECT empno,

    MAX(CASE

    WHEN code = 'S' THEN hrs

    END) AS AccruedVacation,

    [effective date] AS accrued_sick_effective_date,

    MAX(CASE

    WHEN code = 'T' THEN hrs

    END) AS AccruedSick,

    import_date

    FROM tempaccrual T

    GROUP BY empno,

    [effective date],

    import_date

    ) s

    WHERE NOT EXISTS ( SELECT 1

    FROM vacationaccrual v

    WHERE v.empno = s.empno AND v.accrued_vacation = s.accrued_vacation AND v.accrued_sick_effective_date = s.accrued_sick_effective_date AND v.accrued_sick = s.accrued_sick AND v.import_date = s.import_date )

    This works great but when there is null value in any of the fields then the checking fails and keep inserting duplicate rows. Please advise

  • shezi (7/27/2015)


    This works great but when there is null value in any of the fields then the checking fails and keep inserting duplicate rows. Please advise

    Did you try the solution that I posted? How does that work?

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply