Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Import Flat File in SSIS

Posted on 2011-02-15
11
Medium Priority
?
1,047 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 5

Accepted Solution

by:
jijeesh earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
Viewers will learn the different options available in the Backstage view in Excel 2013.

885 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