question

HannahG avatar image
HannahG asked

inserting a .csv file into a database table

Hey, The code below is supposed to insert a .csv file into a database table but it is only creating the table and not posting the records. Kindly help me troubleshoot the code. Thank you. USE [TEST] GO drop table #t_Statement CREATE TABLE #t_Statement ( [TRANS_DATE][nvarchar](1000) NULL , [TRANS_DETAILS][nvarchar](1000) NULL, [TRANS_MONEY_OUT] [nvarchar](1000) NULL, [TRANS_MONEY_IN] [nvarchar](1000) NULL, [TRANS_LEDGER_BAL] [nvarchar](1000) NULL, [TRANS_AVAILABLE_BAL] [nvarchar](1000) NULL) --PRIMARY KEY [TRANS_DETAILS]) BULK INSERT #t_Statement FROM 'C:\StatementScripts\Statement.csv' WITH ( FIELDTERMINATOR = '\t', ROWTERMINATOR = '\n', FIRSTROW = 2 )
tables
8 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

GPO avatar image GPO commented ·
This might not be relevant, but you've specified a tab (\t) as your field terminator, but you've also said it's a csv (comma separated values) file. Is it tabs or commas?
0 Likes 0 ·
Sule avatar image Sule commented ·
Do you get any error? Check your delimiter first. How many rows do you have in your csv file? If you have only one row, your script start importing from row 2 (FIRSTROW = 2). Also, check folder security, do you have access rights? For security options read this https://msdn.microsoft.com/en-us/library/ms188365.aspx
0 Likes 0 ·
HannahG avatar image HannahG commented ·
The code does not return any error, just an empty table. i have 24 rows and yes i have access rights
0 Likes 0 ·
Sule avatar image Sule HannahG commented ·
Could you please copy first 2-3 rows from csv file here?
0 Likes 0 ·
HannahG avatar image HannahG commented ·
28/05/2015 Chq Dep CHQ000094 CHQ1514870770-LEAH AT-DPC 14,778.00 3,308,560.08 29/05/2015 Chq Dep CHQ000002 CHQ1514917840-LEAH AT-DPC 45,000.00 3,353,560.08
0 Likes 0 ·
Sule avatar image Sule commented ·
For test, create table t_Statement_Test in your database. Using import wizard (right click on database -> Tasks -> Import data), for data source choose flat file and choose your csv file, for data destination choose t_Statement_Test table. For data delimiter use Tab and try to import data. Is it works for you?
0 Likes 0 ·
Show more comments

1 Answer

·
Sule avatar image
Sule answered
Try one more time with BULK INSERT, but use ROWTERMINATOR = '0x0a' or ROWTERMINATOR = '\r\n' instead of ROWTERMINATOR = '\n' And add one more option: DATAFILETYPE = 'char'
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

HannahG avatar image HannahG commented ·
- Executing (Error) Messages Error 0xc02020a1: Data Flow Task 1: Data conversion failed. The data conversion for column "Column 0" returned status value 4 and status text "Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard) I got this error after using the import wizard Error 0xc020902a: Data Flow Task 1: The "output column "Column 0" (10)" failed because truncation occurred, and the truncation row disposition on "output column "Column 0" (10)" specifies failure on truncation. A truncation error occurred on the specified object of the specified component. (SQL Server Import and Export Wizard) Error 0xc0202092: Data Flow Task 1: An error occurred while processing file "C:\BankStatementScripts\KcbStatement.csv" on data row 1. (SQL Server Import and Export Wizard)
0 Likes 0 ·
HannahG avatar image HannahG commented ·
Still not working
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.