|
|
Question : Dumping Data from multiple tables into flat files using foreach loop
|
|
I'm trying to dup the data in tables that I have in my database into a flat file. I get the name of tables, pass them to a foreach loop and read the data for each table but the problem is since each tables schema is different the column mapping to the text file causes a failure. Is there anyway to solve this problem?
Thanks in advance.
|
Answer : Dumping Data from multiple tables into flat files using foreach loop
|
|
Declare the variables:
DatabasePath with the value "YourDataBaseName.dbo." ClosingArgs with the value ".txt -T -c" TableName with the value "MyTable" TablesList of type "Object" Direction with value " out C:\"
Add an execute sql task with the sql statement "SELECT name FROM sys.tables" and result set property "Full result set"
In the execute sql task window go to ResultSet group and add one with Resultset Name "0" and Variable Name User::TablesList
Add a foreach loop container and in "Collection" select the enumerator "Foreach ADO enumerator" and bind it to the ADO object source variable: User::TablesList
In variable mappings add a mapping to the User::TableName variable with index 0
Add an execute process task, in the process pane put the Executable property pointing to your bcp program path (like C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe); in the Expressions pane add an expression for Arguments property and then put "@[User::DatabasePath] + @[User::TableName] + @[User::Direction] + @[User::TableName] + @[User::ClosingArgs]"
Run your package... and thats all folks!!!
|
|
|
|