Author |
Topic |
MrBloom
Starting Member
36 Posts |
Posted - 2013-06-14 : 11:46:34
|
HI I have a list of about 1000 table names in a flat file, each on a different line. I would like to use this list to create 1000 sql tables tables with these names. I have created an execute sql task with a dynamic TableName variable which works OK for one table. I then want to place the execute sql task inside a for each loop container and pass each value from the text file list to the TableName variable to create the tables. Any help with this part would be appreciated, thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-14 : 15:21:25
|
Easiest way would be to dump the results of file to a single column table in a data flow task. Follow it up with a execute sql task to create and load an object variable inside ssis using table resultset. You can just use query like select col from temptable. Then connect the output of execute sql task to your for each loop containing the create table sql task. Use for each ADO.NET enumerator and map it to the object variable created. Inside for each loop you need to map the variable created for tablename to get iterative value from object variable during each iteration.so package will look like1.Data Flow task (file -> temptable import)2.create object variable tablenamelist3.execute sql task with query asselect columnname from temptableMake resultset type as full resultsetmap columnname to object variable in resultset tab4.configure foreachloop to use above variable using ADo.NET enumerator5. Map tablename variable inside foreachloop to get iterated value6. Apped variable to your create table statement inside sql task in loop------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MrBloom
Starting Member
36 Posts |
Posted - 2013-06-16 : 08:11:25
|
Thanks very much for this. I tried it and it works. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-16 : 14:17:42
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MrBloom
Starting Member
36 Posts |
Posted - 2013-06-16 : 16:20:42
|
Hi I'm trying to load data into these tables I created but have come up against a problem. I have a source csv file with about 1000 columns with a header in the format quote: Id, column1, column2, column3 ... column1000
I want to split and load the csv file to the 1000 sql server tables I created with the PK ID and one of the csv file fields in each table. Each csv field names matches to a sql table name. I thought I would be able to manage this if I created the tables first but its proving difficult. I tried putting another execute sql tast in the for each loop container with an insert statement, but can't make it work. I have also maybe gone about this the wrong way in creating the tables first and it would have been easier to make each table and load the data on the fly. If you are able to help that would be great, if not don't worry. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-17 : 00:59:39
|
let me understand this firstso you need one table each for each of the columns? and then you want that columns contents to go into the table?I thought your initial post told tablenames are present as row values inside flatfile?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MrBloom
Starting Member
36 Posts |
Posted - 2013-06-17 : 02:58:01
|
Yes, this is true. I originally extracted the column headers from the csv file to a flat file as row values to make it easier. perhaps it was an unnecessary step though. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-17 : 03:45:08
|
quote: Originally posted by MrBloom Yes, this is true. I originally extracted the column headers from the csv file to a flat file as row values to make it easier. perhaps it was an unnecessary step though.
Ok in that case i would have done it like this1.Data Flow task (file -> temptable import)2.create object variable tablenamelist3.execute sql task with query asSELECT COLUMN_NAME FROM INFORMATION_SCHEMA>COLUMNSWHERE TABLE_NAME = 'temptable'AND COLUMN_NAME <> 'PrimaryKeyCOlumnNameHere'Make resultset type as full resultsetmap columnname to object variable in resultset tab4.configure foreachloop to use above variable using ADo.NET enumerator5. Map tablename variable inside foreachloop to get iterated value6. Create a variable to hold sql statement for execute sql task inside the loop. Set EvaluateAsExpression property true for it ad set expression as"SELECT " + @[User::TableName] + " INTO " + @[User::TableName] + " FROM Temptable"7. Put the Execute sql task inside loop, set SQLSourceType as variable and map the above sql statement variable to it.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
MrBloom
Starting Member
36 Posts |
Posted - 2013-06-18 : 06:58:43
|
One again this works very well visakh16. Thanks so much for your expertise, I have learned a lot about looping through name lists. Best Regards |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-18 : 07:15:57
|
you're welcomeglad that i could be of help------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|