Solved

cvs Flat File Source to OLE DB Destination SSIS

Posted on 2014-07-28
6
861 Views
Last Modified: 2016-02-11
Dear Experts;

My brain just ran out of go juice, but I need to query a .csv file to return only DISTINCT records into a SQL 2008 table using SSIS.  I'm cool on using the Data Flow Task to pull all of the records from the .csv file, but can I just query only the DISTINCT records form the .csv flat file?  I have included a screen shot of the Flat File Source Editor and the OLE DB Destination Editor.
FlatandOLE.docx
0
Comment
Question by:wdbates
6 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 40225202
Why necesarily SSIS? you can import csv directly into a (temp) table then just select your DISTINCT records from that table - please see pseudo code below to help you with that and note that the location of the CSV file is relative to the SQL server not client running the query.



--IMPORT
CREATE TABLE Sample-Output.csv
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

--Create CSV file in drive C: with name Sample-Output.csv.txt with following content. The location of the file is C:\Sample-Output.csv.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101
4,Alex,Smith,20040202


--Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK
INSERT Sample-Output.csv
FROM 'c:\Sample-Output.csv.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT DISTINCT * --or your own criteria for DISTINCT here
FROM Sample-Output.csv
GO
0
 

Author Comment

by:wdbates
ID: 40225234
Hello Iochan;

This just one file of many very large files.  Presently I am loading the .csv files into a staging table as seen in the attachments.  After the staging files are loaded I run an editing processing checking for errors, etc. and then I use MERGE to UPDATE or INSERT the record into the final table.  The client performs very little checking and is known for sending duplicate records.  I thought if I just removed them even prior to the staging table that would save some processing time.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 40225263
As far as I'm not aware of anything like that to exists - I mean eliminating duplicates during insert while reading the CSV file you may need and intermediate staging where to put the current csv and select from there the DISTINCT records to be inserted into the Staging or...if you think you can add a PK on the Staging to ignore the duplicates (although this may be costly if you have many columns as part of the PK) IGNORE_DUP_KEY = ON may help you and please see code sample below:


CREATE TABLE dbo.foo (col1 int,col2 sysname PRIMARY KEY WITH (FILLFACTOR=90, IGNORE_DUP_KEY = ON))
GO
INSERT dbo.foo VALUES (1,'Fname')
GO
INSERT dbo.foo VALUES (1,'Fname')
GO
INSERT dbo.foo VALUES (1,'Fname')
GO
--gives only  

(1 row(s) affected)
Duplicate key was ignored.

(0 row(s) affected)
Duplicate key was ignored.

(0 row(s) affected)
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 5

Expert Comment

by:rtay
ID: 40225692
By Distinct records, do you mean no duplicate STUD_ID's to be entered into the database?  If so, use a lookup Lookup Component to check for STUD_ID before importing into the DB.
0
 
LVL 14

Accepted Solution

by:
Vikas Garg earned 250 total points
ID: 40226277
Hi,

You can use the Sort Transformation which will give you option to remove duplicate records and thus you can get distinct records from the CSV file.

Sort Transformation Image
0
 

Author Closing Comment

by:wdbates
ID: 40227018
Dear Vikas Garg;

Your solution was great and thank you for the screen shot.  I forgot all about the Sort Transformation.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now