Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Errors with bcp and bulk insert

Author  Topic 

dcs
Starting Member

6 Posts

Posted - 2011-09-16 : 06:47:41
I am having a csv file which I have to upload on my database. The table is as follows:-

CREATE TABLE [dbo].[Out_orders_Table2](
[CompanyID] [nchar](3) NULL,
[ItemCode] [nchar](9) NULL,
[Description] [nchar](38) NULL,
[SupplierAccount] [nchar](9) NULL,
[PONumber] [nchar](8) NULL,
[POLineNumber] [numeric](3, 0) NULL,
[OutstandingQty] [numeric](6, 0) NULL,
[DueDate] [date] NULL,
[StockRoom] [nchar](3) NULL,
[PriorityFlag] [nchar](3) NULL
) ON [PRIMARY]

GO

The data file is like this:-

MK,12345678,ABC 123lr RK Finish abcde abcdef ,ABCDEFGH,A123456,1,1234,18/09/2010,W1,

CD,98765432,RR CTR APPLE JAM AND JELLY KITS ,ABCDEFGH,A654321,1,25000,08/06/2011,W1,

AB,123456,Ab1 Class AB2 3pk R/Ctn M&K Eu Oct07 ,ABCDE007,P123456,8,1200,11/09/2011,CD,

EF,123456,2 x Tennis balls, Rackets and bags,TNSRCKBL,T763781,3,2800,10/08/2011,DL,


Please note,

1. In row 4 there is a comma in the field. And I have to accomodate them and also they are not having double quotes.
2. I have used a comma and space delimited file as well. to allow the fields having comma in them.
3. Also the last column 'Priority Flag' doesnt have any data in the CSV file, so the column before it ends like this.. ,CD,
4. I am using SQL SERVER 2008. I am using bcp format file with version 8.0 because it wasnt working for 10.0

Format File :-

8.0

10

1 SQLNCHAR 0 3 "" 1 CompanyID ""
2 SQLNCHAR 0 9 "" 2 ItemCode ""
3 SQLNCHAR 0 38 "" 3 Descritpion ""
4 SQLNCHAR 0 9 "" 4 SupplierAccount ""
5 SQLNCHAR 0 8 "" 5 PONumber ""
6 SQLNUMERIC 0 3 "" 6 POLineNumber ""
7 SQLNUMERIC 0 6 "" 7 OutstandingQty ""
8 SQLDATE 0 10 "" 8 DueDate ""
9 SQLNCHAR 0 3 "" 9 StockRoom ""
10 SQLNCHAR 0 3 "\n" 10 PriorityFlag ""

This format file I manually created. I have already tried:-
1. Using an auto generated format file using bcp utility. It gave errors.
2. In the above file using SQLCHAR instead of SQLNCHAR.
3.using "/r/n" as field terminator.
using bcp in command prompt:
bcp TestDB.dbo.Orders_Table2 in E:\Testing\OSOTest1.csv -f E:\Testing\OrdersF1.fmt -T -F 2 -b 100
Errors:-
Starting copy...
SQLState = 22008, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Invalid date format
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Numeric value out of range
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]String data, right truncation
BCP copy in failed
======================================================================
I used script to do the same thing. The stored procedure is as follows:-
USE [TestDB]
GO
CREATE PROCEDURE [dbo].[example_load_OS_file]

AS
DECLARE @filename varchar(30) = 'OSOTest1.csv'
DECLARE @TSQL varchar(2000)
SET @TSQL = '
BULK INSERT [Orders_Table2]
FROM '''+'E:\Testing\'+@filename+''' '
SET @TSQL = LTRIM(RTRIM(@TSQL))

EXEC(@TSQL+' WITH (formatfile = '''+'E:\Testing\OrdersF1.fmt'+''')')

Errors I receive when I execute this stored procedure:-
Msg 7399, Level 16, State 1, Line 2
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 2
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

Should I use openrowset or SSIS for this. I have never worked with them before, and all other CSV files I have uploaded are using a stored procedure.. but this one doesnt seem to work.
Plz advice.. Thanks in advance.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-11-16 : 05:14:20
set dateformat dmy
bcp TestDB.dbo.Orders_Table2 in E:\Testing\OSOTest1.csv -f E:\Testing\OrdersF1.fmt -T -F 2 -b 100


The above will solve date related error and then proceed further

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -