I have CSV file with three columns.
sno sname quantity
--- ----- --------
1 aaa 23
2 bbb null
3 ccc 34
4 ddd ddd
5 eee xxx
6 fff 87
Table in the SQL Server database is as following/
CREATE TABLE csvtable
( sno int
, sname varchar(100)
, quantity numeric(5,2)
)
I created an SSIS package to import csv file data into the database table. I am getting an error during package execution because the quantity is a string. I created another table to store the invalid data.
CREATE TABLE wrongcsvtable
( sno nvarchar(10)
, sname nvarchar(100)
, quantity nvarchar(100)
)
In the csvtable, I would like to store the following data.
sno sanme quantity
--- ------ --------
1 aaa 23
3 ccc 34
6 fff 87
In the wrongcsvtable, I would like to store the following data.
sno sanme quantity
--- ------ --------
2 bbb null
4 ddd ddd
5 eee xxx
Could someone point me in the right direction to achieve the above mentioned output?
Here is one possible option. You can achieve this using the
Data Conversiontransformation within theData Flow Task. Following example shows how this can be achieved. The example uses SSIS 2005 with SQL Server 2008 database.Step-by-step process:
Create a file named
FlatFile.CSVand populate it with data as shown in screenshot #1.In the SQL database, create two tables named
dbo.CSVCorrectanddbo.CSVWrongusing the scripts provided under SQL Scripts section. The fields in the tabledbo.CSVWrongshould have the data types VARCHAR or NVARCHAR or CHAR so that it can accept the invalid records.On the SSIS package, create an OLE DB connection named SQLServer to connect to SQL Server database and create a Flat File Connection named CSV. Refer screenshot #2. Configure the flat file connection CSV as shown in screenshots #3 – #7. All the columns in the flat file connection should be configured as string data type so that the package doesn’t fail while reading the file.
On the Control Flow tab of the package, place a
Data Flow Taskas shown in screenshot #8.On the Data Flow tab of the package, place a
Flat File Sourceand configure it as shown in screenshots #9 and #10.On the Data Flow tab of the package, place a
Data Conversiontransformation and configure it as shown in screenshot #11. Click on theConfigure Error Outputand change the Error and Truncation column values from Fail component to Redirect row. Refer screenshot #12.On the Data Flow tab of the package, place an
OLE DB Destinationand connect the green arrow from Data Conversion to this OLE DB Destination. Configure the OLE DB Destination as shown in screenshots #13 and #14.On the Data Flow tab of the package, place another
OLE DB Destinationand connect the red arrow from Data Conversion to this OLE DB Destination. Configure the OLE DB Destination as shown in screenshots #15 and #16.Screenshot #17 shows the Data Flow Task once it has been completely configured.
Screenshot #18 shows data in the tables before the package execution.
Screenshot #19 shows package execution within Data Flow Task.
Screenshot #20 shows data in the tables after the package execution.
Hope that helps.
SQL Scripts:
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
Screenshot #11:
Screenshot #12:
Screenshot #13:
Screenshot #14:
Screenshot #15:
Screenshot #16:
Screenshot #17:
Screenshot #18:
Screenshot #19:
Screenshot #20: