Solved

Import Flat File in SSIS

Posted on 2011-02-15
11
1,019 Views
Last Modified: 2012-05-11
My flat file is ragged right with no column headings but has multiple headers, detail, footers.
header
detail
footer

header
detail
footer

I need to import the detail only and not the header/footers.  I tried to import all and later delete the header and footers but not all the records are getting into my sql table.  Does anyone know how this can be accomplished.  I am new to SSIS and do not know how to set up a conditional split since I've read that this may be a solution.  Appreciate any help ... ps this is urgent.
0
Comment
Question by:bar0822
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 5

Expert Comment

by:jijeesh
ID: 34901410
Are your headr, details and footer info  repeating?  
Can you send a sample input file and what are you expect to extract.

0
 
LVL 40

Expert Comment

by:lcohan
ID: 34901423
If you can do it directly in SQL query the example below should help just keep in mind the path and file is relative on the SQL server box not the client where you run the query:

--Assumes:Usage : exec sp_readTextFile 'c:\autoexec.bat'
--**************************************

Create proc sp_readTextFile @filename sysname
as


    begin
    set nocount on
    Create table #tempfile (line varchar(8000))
    exec ('bulk insert #tempfile from "' + @filename + '"')
    select * from #tempfile
    drop table #tempfile
End
go

0
 

Author Comment

by:bar0822
ID: 34909821
here is a sample
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 5

Accepted Solution

by:
jijeesh earned 500 total points
ID: 34912877
Attached a sample dtsx package. (you need to rename the extension from .txt to .dtsx). Hope this will be useful.
Copy-of-Package.txt
0
 

Author Comment

by:bar0822
ID: 34928722
I will try it and let you know. thanks
0
 

Author Comment

by:bar0822
ID: 34928809
Can you send me the solution file so I can take a look at it?
0
 

Author Comment

by:bar0822
ID: 34928842
I am running this from SSIS - how do I use your proc sp_readTextFile @filename sysname.

How does that omit the header and footer lines from the text file?
0
 
LVL 40

Expert Comment

by:lcohan
ID: 34929640
SQLCMD has many switches and I only gave an exmaple for you. you should look for more details about SQLCMD at http://msdn.microsoft.com/en-us/library/ms162773.aspx and use the switches you need for your purpose like:

-hheaders
Specifies the number of rows to print between the column headings. The default is to print headings one time for each set of query results. This option sets the sqlcmd scripting variable SQLCMDHEADERS. Use -1 to specify that headers must not be printed. Any value that is not valid causes sqlcmd to generate an error message and then exit.

-scol_separator
Specifies the column-separator character. The default is a blank space. This option sets the sqlcmd scripting variable SQLCMDCOLSEP. To use characters that have special meaning to the operating system such as the ampersand (&), or semicolon (;), enclose the character in quotation marks ("). The column separator can be any 8-bit character.

0
 

Author Comment

by:bar0822
ID: 34932963
thank you so much for all your time and help - I did not know this existed but there will def be a time when I will need to use.  sorry, I did not explain myself correctly - The text file does not have column headers - it is a flat ragged right file - I was looking to extract information about the data in the flat file e.g.
Header000 20110101
Detail information here
Trailer000 4,222 records
Header0000 20110101
Detail information here
Trailer000 3,111 records
I need to extract only the Detail information and send to SQL table through a SQL Execute Task, need to add a count to those records.  I was thinking of a conditional split - extract header/trailer data and send to a file, then count the rows in detail.  I am new to ssis and not sure how to write this.
0
 

Author Comment

by:bar0822
ID: 35097615
here is new file posted revised
Revised-Text-Document.txt
0
 
LVL 40

Expert Comment

by:lcohan
ID: 35097705
--Ok so let's leave the SP for now and just try in SQL to get what you need

Create table #tempfile (line varchar(8000))
exec ('bulk insert #tempfile from "' + 'C:\FolderName\FileName.txt' + '"')
select * from #tempfile where line NOT LIKE 'Header%' or line NOT LIKE 'Trailer%'
drop table #tempfile


if this is what you want then you can put the code in a SSIS T-SQL step and add a INSERT statement into your DB table from the temp table. You obviously can adjust the datatype,number of columns ETC now is all in SQL and easy to work with.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 56
date diff with Fiscal Calendar 4 75
SSRS: Why is Visual Studio stripping these properties? 2 39
denied execute as 13 25
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question