Link to home
Start Free TrialLog in
Avatar of bar0822
bar0822

asked on

Import Flat File in SSIS

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.
Avatar of jijeesh
jijeesh
Flag of India image

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

Avatar of lcohan
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

Avatar of bar0822
bar0822

ASKER

here is a sample
ASKER CERTIFIED SOLUTION
Avatar of jijeesh
jijeesh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bar0822

ASKER

I will try it and let you know. thanks
Avatar of bar0822

ASKER

Can you send me the solution file so I can take a look at it?
Avatar of bar0822

ASKER

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?
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.

Avatar of bar0822

ASKER

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.
Avatar of bar0822

ASKER

here is new file posted revised
Revised-Text-Document.txt
--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.