Domanda

How to do regular updates to a database table in SSIS. The table has foreign key constraints. I have a package running every week, and I have to update the data in the table from a flat file. Most of the contents are the same with update values and other new rows.

UPDATE : My data file contains updated contents ( some rows missing, some rows added, some modified ). The data file does not have the Primary keys ( I create the primary keys when I first bulk insert the data from the data file ), on subsequent SSIS package runs, I need to update the table with new data file contents.

e.g.

table
---------------------------------------------
1    Mango   $0.99
2    Apple   $0.59
3    Orange  $0.33


data file 
---------------------------------------------
Mango   0.79
Kiwi    0.45
Banana  0.54

How would I update the table with data from the file. The table has foreign key constraints with other tables.

È stato utile?

Soluzione

another approach, to load massive group data instead of dealing row by row:

On database

  • create an staging table (e.g. StagingTable [name], [price])
  • Create a procedure (you may need to change the objects names, and add transaction control and error handling etc just a draft):

    create procedure spLoadData
    

    as begin

    update DestinationTable
       set DestinationTable.Price = StagingTable.Price
      from DestinationTable
      join StagingTable
        on DestinationTable.Name = StagingTable.Name
    
    insert into DestinationTable
          (Name, Price)
    select Name, Price
      from StagingTable
     where not exists (select 1 
                         from DestinationTable
                        where DestinationTable.name = StagingTable.Name)
    

    end

On SSIS

  • Execute SQL Task with (truncate [staging_table_name])

  • Data Flow task transferring from your Flat File to the Staging Table

  • Execute SQL Task calling the procedure you created (spLoadData).

Altri suggerimenti

Following are the few thoughts/steps:

  • Create a Flat File Connection manger.
  • Take Data flow task.
  • Create Flat File Source with connection manager just created.
  • Take lookup transformation(s) as many as you need to get FK values based on your source file values.
  • Take a lookup transformation after all above lookups, to get all values from Destination table.
  • Keep Conditional split and compare source values and destination values.
  • If all columns matched then UPDATE, else INSERT.
  • Map above conditional split results accordingly to OLEDB Destnation/OLEDB Command.

Give a try and let me know the results/comments.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top