.. oh and I'll ask someone to add this to the SSIS section too.
Main Topics
Browse All TopicsI have 4 SQL tables for each type of record set up that capture data that I need to export to a single text file.
The format of the file is as follows:
1st line is a File Header
2nd line is a Detail
3rd is Distribution
Last line is a File Trailer
Each of the line types have different fields with different vales. I am trying to use SSIS and not BCP. I was able to get one table with one data type exported with the Flat File Destination. But, I am having a major problem getting to the next step with putting it all together. I know a Foreach Loop probably needs to be in there?
Any clues would be much appreciated.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If I understand your question correctly, and you are using SSIS with a Data Flow Task, I believe that you need a Merge Join transformation.
Merging multiple data sources with the MERGE JOIN task in SQL Server Integration Services SSIS
http://www.mssqltips.com/t
Try a query that uses UNION ALL in a single Data Flow Task, and make sure that you have indicated that your data is sorted (the IsSorted flag and SortKeys in the Advanced Editor).
Your data sources may prefer to be just a couple of columns to indicate order, and a long string column for the file contents. Queries a bit like:
SELECT RecordID, 1 as RowOrder, Whatever + You + Need + For + The + File as FileContents
FROM FileHeaderTable
UNION ALL
SELECT RecordID, 2 as RowOrder, Whatever + Else + You + Need as FileContents
FROM DetailTable
UNION ALL
SELECT RecordID, 3 as RowOrder, Stuff + For + Distribution as FileContents
FROM DistributionTable
UNION ALL
SELECT RecordID, 3 as RowOrder, Whatever + Else + You + Need as FileContents
FROM TrailerTable
ORDER BY RecordID, RowOrder
Then just dump the FileContents straight into the file. :)
Rob
Business Accounts
Answer for Membership
by: nmcdermaidPosted on 2009-11-02 at 16:04:51ID: 25725071
The basic problem is the the flat file destination expects a single definition of the file - i.e. 7 columns, the whole way through. The data you are exporting has different definitions per line.
There are three ways that I can think of to do this:
1. Get tricky with .Net scripts and dynamic columns (Personally I don't think its worth the complexity)
2. Define a file output with one column. Append and order all of the required data internally (in the source SQL) and export it to one column. (it looks like multiple columns in the file)
3. Create four data flows. Each one exports the specific data definition to a work file. Using a for loop and some conditional flows (run by some SQL which controls the whole lot), the four data flows are selectively run to export the data. After a data flow is finished, a copy command appends the work file to the final file. When the master SQL us finished, you have a final file which contains all of the data in the right order
If solution 3 interests you let me know and I can take it further. Have a think about the 'master' SQL that you will need to run which basically returns a record for each section in the output, along with some kind of filter that we can use to run some more SQL to get the data out.
I am interested if anyone else has any other (better) options.