Home Forums SQL Server 7,2000 General Importing data from txt file and updating Table RE: Importing data from txt file and updating Table

  • One way is to import the file into a temporary table and then process that temporary table to insert or update records in the master table.

    Depending on how complex the data is, you might get away with a simple insert statement:

    insert <master_table>

    select <col1>, <col2>

    from <temporary_table>

    where not exists (select * from <master_table> where <temporary_table>.<Primary_Key> = <master_table>.<primary_key> )

    ie insert a row where it does not exist already.

    Updates would be update row where exists already.

    If there is more processing to do on each row, such as field validation or inserts into other tables then things get a bit more complicated but the approach is still valid.

    There are several ways of controlling this process. One way is using DTS (personally I have never got to grips with it). Another way is using TSQL (CREATE temp table, BULK INSERT update file into temp table, INSERT new rows, UPDATE existing rows, DROP table).

    Either way, you can put it into a job and then schedule it to run as required.

    Jeremy