Insert into Select - only new or changed rows

  • Good morning, 
     I have a "insert into select" script that copies records from a database I don't have full access to,  to a staging table. The script seems to work fine except I'm having trouble getting only new or changed rows. any suggestion on the best way to go about this ? my script is attached.


    Thank you

  • If you do an outer join between the source table and the target table, you can insert only the rows that don't already exist.

    John

  • Do you really want to insert changed rows?  Usually people want to update changed rows and insert new rows.  As for identifying which are new/changed, I like EXCEPT unless your table has only a few columns.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Thanks Guys...True I wont want to insert changed row should I do a merge ?   
    I was hoping to keep the script as is...the outer join takes me away from that.

  • gjoelson 29755 - Friday, September 7, 2018 9:10 AM

    Good morning, 
     I have a "insert into select" script that copies records from a database I don't have full access to,  to a staging table. The script seems to work fine except I'm having trouble getting only new or changed rows. any suggestion on the best way to go about this ? my script is attached.


    Thank you

    Are your co_num in ascending order?  Maybe you could add something like this to your select?


    AND co_num > (select max(co_num) from customerorders)

  • gjoelson 29755 - Friday, September 7, 2018 9:28 AM

    Thanks Guys...True I wont want to insert changed row should I do a merge ?   
    I was hoping to keep the script as is...the outer join takes me away from that.

    Merge is slower than an UPDATE followed by an INSERT. So you should really have two statements to do it.
    What column(s) do you match on to do an UPDATE or an INSERT?

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

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