Dynamic table create and load data in ssis

2019-02-21 04:38发布

问题:

I have small doubt in ssis, text file have location data based on location need to create table dynamicaly in sql server and load related location data using ssis package, source text file have sample data like below

id | name | location 1 | abc |Hyd 2 | banu |chen 3 | jai |bang 4 | hari |Hyd 5 |nani |Hyd 6 | banti |bang 7 |kali |chen

based on this text file load we need to create location tables dynamically and load related data.because of some time may have more location in text file that situation we need to create table dynamically and load it.please tell me how to achive this task using ssis.we need foreach loop container but don't how to achive this.please tell me to solve this issue

回答1:

1) DFT task (Get distinct locations and respective table names)

  • Create first DFT to get distinct locations from file. Add aggregate transform and give input column as Location and select oparation as GroupBy which will provide you distinct locations in file
  • Then, add derived transformation to get the locations and its respective table name TableName - (DT_STR,50,1252)("dbo.LocationList" + location) Location - (DT_STR,50,1252)location
  • Add recordset destination and store the TableName and Location values in variable (name = Locations) of type object

2) Foreach loop container task(Create tables and store data)

  1. Foreach loop container configuration -

    • Select enumerator as "Foreach ADO Enumerator"
    • select source variable as "Locations"
    • Enumerator mode - Rows in first table
    • In variable mappings get location and respective table name in two variables "location" and "Locationtable" respectively.
  2. Add SQL Task in container (to create table if it is not present)

    • Provide SQL source expression as -

    "If Object_Id('" + @[User::Locationtable] + "') IS NULL CREATE TABLE " + @[User::Locationtable] + " ( id int, Name Varchar(50) )"

  3. Add DFT task in container (to import data into respective tables)

    • Add flat file source to get load data from same source file
    • Add conditional split and add expression "location == @[User::location]" to get matching output
    • Add OLEDB destination and set access mode as "OpenRowset Using FastLoad From Variable"
    • Select variable "User::Locationtable"

Note - you will need to provide default value to "User::Locationtable" variable with table name which is present on database and has same schema, so that you can map the columns in second DFT task.