Updating records vs Insert records

  • I am trying to update a table with store information for thousands of stores. I was given a file with the information to update. Some of those entries in the file are not in the database, so there I have to an insert record. What is the easiest way to do this to write a script to update just the records that need updating and inserting new records in one script? Thanks.

  • If you do not like cursor, you may do it as below.

    1. Make a temporary tabe;

    2. Populate data on it;

    3. Retrieve new data by comparing the new table and the existing;

    4. Insert into the new table.

  • UPDATE A

    SET ....

    FROM TableA A

    INNER JOIN TableB B ON B.KeyCol = A.KeyCol

    INSERT INTO TableA

    (columns list)

    SELECT B.Columns list

    FROM TableB B

    WHERE NOT EXISTS (

    select 1 from TableA A where B.KeyCol = A.KeyCol)

    _____________
    Code for TallyGenerator

  • Thanks folks,

    The idea and the SQL provided helped

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

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