Link to home
Start Free TrialLog in
Avatar of Rayne
RayneFlag for United States of America

asked on

Import CSV Using SSIS - Column Format Issue

Hello All,
I am trying to import a simple CSV file into SQL Server using the SSIS. I have been unsuccessful so far. Spent hours but cant figure out. Please assist.

Please Check the attached Word Document for details and Screenshots


Step 1

Created tables with this query…

CREATE TABLE [dbo].[TestCSVUpload](
[OrderDate] [nvarchar](255) NULL,
      [Region] [nvarchar](255) NULL,
      [Rep] [nvarchar](255) NULL,
      [Item] [nvarchar](255) NULL,
      [Units] [numeric](18, 0) NULL,
      [Cost] [numeric](18, 0) NULL,
      [Total] [numeric](18, 0) NULL
) ON [PRIMARY]

GO


Step2
Placed [testIncomingTbl.csv] in a certain folder for connection string (attached the csv file in question)
C:\incomingCSVFolder


Step3
a. In SSIS, added a Flat File Source
Created connection manager for it in SSIS like this>>
 


Step4:
Create OLE DB Destination in SSIS


Step5:
Connect the green arrow from Flat File Source to this [OLE DB Destination]
 


Step6:
Under the OLE DB Destination Editor, assigned the table as the target destination
 
Now this is what I am getting now >>>>
 
Spent hours trying to change the column formats but to no avail, this keeps showing up.

Provided the source CSV File in the question and also given the Query need to create a table in server for getting this csv data set.

Not sure where I am missing anything…

Thanks
R
csvimportIssue.docx
testIncoming.csv
Avatar of Simone B
Simone B
Flag of Canada image

How did you try to convert the data? It seems what you need is a Data Conversion task immediately after the flat file source (see attached). You should be able to use it to convert the strings.

However, wouldn't you want your OrderDate in the SQL table to be of datetime data type?
Capture.JPG
Avatar of lcohan
If it is truly only a csv you could do it via SQL code directly like in example below - just adapt to your table/csv structure and keep in mind the csv path is relative to the SQL Server box not client:

--IMPORT
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

--Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.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 CSVTest
FROM 'c:\csvtest.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
SELECT *
FROM CSVTest
GO

--EXPORT data to CSV
--with BCP:

exec master..xp_cmdshell N'sqlcmd -E -Q"bcp "select ''ProductID'', ''Name'', ''ProductNumber'' union all select convert(varchar(10), ProductID), Name, ProductNumber from AdventureWorks2008R2.Production.Product" queryout "c:\temp\product.csv" -c -T >nul'--, no_output

--with sqlcmd
exec master..xp_cmdshell N'sqlcmd -E -Q"set nocount on;select * from DBname.dbo.table_name" -h-1 -s"," -W -o"c:\outputTEST.csv"'--, no_output


GO
Avatar of Rayne

ASKER

Hello to All Experts,

Thanks for your help so far. Greatly appreciated. I am newbie to SSIS and Server stuff so please bear with me :(

Buttercup1-
Can you show me how to insert that conversion thing in SSIS?


Icohan –

“csv path is relative to the SQL Server box not client”
I am getting this error when trying to do the bulk insert….so how you do the above thing as you mentioned?
The CSV file is stored in my C drive

Error:
Cannot bulk load because the file “C:\incomingCSVFolder" could not be opened. Operating system error code 5(Access is denied.).
Avatar of Rayne

ASKER

I am an admin in the DB
"The CSV file is stored in my C drive"


Is the CSV file from C:\incomingCSVFolder ON the SQL Server computer? Or you run it against localy installed SQLServer?
If not you must put it in there...
If you run the SQL code on the SQL server computer make sure the account (sql login) running the query has access to that folder
ASKER CERTIFIED SOLUTION
Avatar of Simone B
Simone B
Flag of Canada 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 Rayne

ASKER

Is the CSV file from C:\incomingCSVFolder ON the SQL Server computer? Or you run it against localy installed SQLServer?
Not sure if I unserstood. But I have a sql server installed in my computer. When I open the SSMS, I just login to the Main server using windows authentication and do my stuff there.The CSV file is my local C Drive.

If not you must put it in there...
If you run the SQL code on the SQL server computer make sure the account (sql login) running the query has access to that folder
Yes my computer login has access to that folder.
Avatar of Rayne

ASKER

I also created a Data conversion object but have question regarding that. Check attached,
Thanks
csvimportIssue201.docx
"I also created a Data conversion object but have question regarding that."

1. Change the data types and lengths to match what is showing in my last attachment.

2. Modify the Mappings in your destination as shown in my last attachment.
Avatar of Rayne

ASKER

Made chnges to conversion object but still issue is there
conversionChanges.png
Please have another look at the document I sent.
Avatar of Rayne

ASKER

Hello Buttercup1, i have attached my observations.
Its only writing 12 rows instead of 43 rows to the DB table
cantWriteAll.docx
Avatar of Rayne

ASKER

not sure whats causing it....
Avatar of Rayne

ASKER

is the data bad or something else?
Avatar of Rayne

ASKER

ok so far you helped me to create the conversion object. So I rewarded you the point. But now the table addition is happening only for few rows, not the entire set of source rows, why?
New Question:

https://www.experts-exchange.com/questions/28001147/SSIS-Conversion-Error.html
Avatar of Rayne

ASKER

create the destination table like this


CREATE TABLE [dbo].[TestCSVUpload](

      [OrderDate][datetime] NULL,
      [Region][nvarchar](255) NULL,
      [Rep][nvarchar](255) NULL,
      [Item][nvarchar](255) NULL,
      [Units][numeric](18, 0) NULL,
      [Cost][numeric](18, 0) NULL,
      [Total][numeric](18, 0) NULL,

      
) ON [PRIMARY]

GO
My sincere apologies, I was away for the weekend and not checking email. But I see you had this answered in another post. I hope it's all working for you now.