SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 import table names from flat file list to variable
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

MrBloom
Starting Member

United Kingdom
32 Posts

Posted - 06/14/2013 :  11:46:34  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 06/14/2013 :  15:21:25  Show Profile  Reply with Quote
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

Edited by - visakh16 on 06/14/2013 15:24:35
Go to Top of Page

MrBloom
Starting Member

United Kingdom
32 Posts

Posted - 06/16/2013 :  08:11:25  Show Profile  Reply with Quote
Thanks very much for this.
I tried it and it works.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/16/2013 :  14:17:42  Show Profile  Reply with Quote
welcome

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

MrBloom
Starting Member

United Kingdom
32 Posts

Posted - 06/16/2013 :  16:20:42  Show Profile  Reply with Quote


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

India
52249 Posts

Posted - 06/17/2013 :  00:59:39  Show Profile  Reply with Quote
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

United Kingdom
32 Posts

Posted - 06/17/2013 :  02:58:01  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 06/17/2013 :  03:45:08  Show Profile  Reply with Quote
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

United Kingdom
32 Posts

Posted - 06/18/2013 :  06:58:43  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 06/18/2013 :  07:15:57  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000