Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 import table names from flat file list to variable

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 like

1.Data Flow task (file -> temptable import)
2.create object variable tablenamelist
3.execute sql task with query as
select columnname from temptable
Make resultset type as full resultset
map columnname to object variable in resultset tab
4.configure foreachloop to use above variable using ADo.NET enumerator
5. Map tablename variable inside foreachloop to get iterated value
6. Apped variable to your create table statement inside sql task in loop


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MrBloom
Starting Member

36 Posts

Posted - 2013-06-16 : 08:11:25
Thanks very much for this.
I tried it and it works.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-16 : 14:17:42
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-17 : 00:59:39
let me understand this first

so 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 MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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.
Go to Top of Page

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 this

1.Data Flow task (file -> temptable import)
2.create object variable tablenamelist
3.execute sql task with query as
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA>COLUMNS
WHERE TABLE_NAME = 'temptable'
AND COLUMN_NAME <> 'PrimaryKeyCOlumnNameHere'
Make resultset type as full resultset
map columnname to object variable in resultset tab
4.configure foreachloop to use above variable using ADo.NET enumerator
5. Map tablename variable inside foreachloop to get iterated value
6. 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 MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-18 : 07:15:57
you're welcome
glad that i could be of help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -