Link to home
Start Free TrialLog in
Avatar of uma_
uma_

asked on

mssql 2008 query

We need a SQL script to upload a tab separated file into a mssql server 2008 database.

Put together the script sample below - example only, it doesn't work but you get the idea

Bulk insert Mobile_Phones_and_Accessories
from 'H:\STWDATA\FTPExtracted\finalextract\Clothing_and_Accessories_1.txt'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n',
CODEPAGE='TEXT',
DATAFILETYPE='char',
FIRSTROW = 2,
KEEPNULLS
)
Avatar of James Murrell
James Murrell
Flag of United Kingdom of Great Britain and Northern Ireland image

>> it doesn't work but you get the idea

it's working and am able to BULK INSERT with your query.  Not sure what do you mean by doesn't work.

Your syntax is looking good.

http://msdn.microsoft.com/en-us/library/ms188365.aspx
Hi uma,

First of all you need to specify the codepage. 'TEXT' is no good there. It has to match coding of your file.

just in case here is more information about bulk insert
http://msdn.microsoft.com/en-us/library/ms188365.aspx

Avatar of uma_
uma_

ASKER

shall i need to change the line
CODEPAGE='TEXT',
it depends on your file and system settings. explicit codepage should be better.
But if Sharath says he/she was able to insert the data with your query than maybe there is something wrong with your data file?

Could you provide any error messages?
Avatar of uma_

ASKER

sorry need to go ,i will get back to u soon

Thanks for ur help..
Avatar of uma_

ASKER

The query in trying is

Bulk insert SDCData
from 'C:\WORKDRIVE\SHOPTHEWEB_DROPBOX\My
Dropbox\ShopTheWeb\bulkupload\mobile_phones_and_accessories.txt'
 WITH
   (
       FIELDTERMINATOR = '\t',
       ROWTERMINATOR = '\n',
       CODEPAGE='TEXT',
         DATAFILETYPE='char',
       FIRSTROW = 2,
       KEEPNULLS
       )


Table structure:

CREATE TABLE [dbo].[SDCData](
       [OFFER_ID] [varchar](25) NULL,
       [OFFER_TITLE] [varchar](96) NULL,
       [PRICE] [varchar](50) NULL,
       [OFFER_DESCRIPTION] [varchar](1024) NULL,
       [MERCHANT_ID] [varchar](50) NULL,
       [MERCHANT_NAME] [varchar](18) NULL,
       [MERCHANT_SKU_NUMBER] [varchar](21) NULL,
       [SDC_PRODUCT_ID] [varchar](50) NULL,
       [MPN] [varchar](14) NULL,
       [CATEGORY_NAME] [varchar](24) NULL,
       [STOCK] [varchar](8) NULL,
       [CATEGORY_ID] [varchar](50) NULL,
       [STOCK_DESCRIPTION] [varchar](76) NULL,
       [IMAGE_URL] [varchar](97) NULL,
       [MAX_IMG_WIDTH] [varchar](50) NULL,
       [MAX_IMG_HEIGHT] [varchar](50) NULL,
       [SHIPPING_RATE] [varchar](50) NULL,
       [SHIPPING_WEIGHT] [varchar](50) NULL,
       [ZIP_CODE] [varchar](20) NULL,
       [MANUFACTURER] [varchar](14) NULL,
       [ATTRIBUTES] [varchar](136) NULL,
       [MIN_CATEGORY_BID] [varchar](50) NULL,
       [MERCHANT_CATEGORY_BID] [varchar](50) NULL,
       [OFFER_URL_MIN_CATEGORY_BID] [text] NULL,
       [OFFER_URL_MERCHANT_CATEGORY_BID] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Mobile-Phones-and-Accessories.zip
The problem is not within the query but within the file.
In your file at the end of each line you have Line Feed character only (U+000A)

In your query you are using '\n' which is interpreted as native new line sequence on Windows OS which is CR+LF (U+000D U+000A).

The simplest solution is to open the file and replace all LF with CRLF then the query will work as expected. Because the file is rather big I recommend using something like Notepad++ (that is what I used to solve the problem on my local machine) In search string you need to put

\n

and replace it with

\r\n

Regards
Lof
Avatar of uma_

ASKER

Hi ,

I opened the file .WHen i serach for the word /n it throws no match found

Please advice
ASKER CERTIFIED SOLUTION
Avatar of lof
lof
Flag of United Kingdom of Great Britain and Northern Ireland 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 uma_

ASKER

I tried ur method still not working...
Could you be more specific? What is not working?
I took your file and tried your query. It didn't work. Then I replaced all \n with \r\n and it worked.
Now I have uploaded the file I managed to import for you to try.

Please give some more details about 'not working...'

Is the file I uploaded not working?
Do you have problems converting \n to \r\n
Can you verify if proper new line characters are in the file you are trying to upload?
Have you installed Notepad++ or do you have any other text editor capable of doing more advanced things than Notepad.exe?
Avatar of uma_

ASKER

I didnt refresh my page before ur post...working on ur solution
Avatar of uma_

ASKER

I saved ur file and tried the query

Bulk insert SDCData
from 'K:\mssql$10\Mobile-Phones-and-Accessories1.txt'
 WITH
   (
       FIELDTERMINATOR = '\t',
       ROWTERMINATOR = '\n',
       FIRSTROW = 2,
       KEEPNULLS
       )

Not working...
What is not working

Please give some more details about 'not working...'

Is the file I uploaded not working?
Do you have problems converting \n to \r\n
Can you verify if proper new line characters are in the file you are trying to upload?
Have you installed Notepad++ or do you have any other text editor capable of doing more advanced things than Notepad.exe?
Avatar of uma_

ASKER

When i tried ur file i am getting this error.Did u import it successfuly?

Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 157, column 2 (OFFER_TITLE).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 167, column 2 (OFFER_TITLE).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 177, column 2 (OFFER_TITLE).
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 181, column 20 (MANUFACTURER).
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 201, column 20 (MANUFACTURER).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 202, column 2 (OFFER_TITLE).
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 261, column 2 (OFFER_TITLE).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 265, column 2 (OFFER_TITLE).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 299, column 2 (OFFER_TITLE).
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 316, column 2 (OFFER_TITLE).
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 327, column 21 (ATTRIBUTES).
Msg 4865, Level 16, State 1, Line 1
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 1
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 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Ok,

First try adding

LASTROW = 100,

just below FIRSTROW

that will show you that  the data can be imported that way. That is how I tested it, not to import all the data into my database. Now when I tried importing more I have errors. The data is not well formated, the data in file.

the errors are as follow

Bulk load data conversion error (truncation) for row 157, column 2 (OFFER_TITLE).
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 167, column 2 (OFFER_TITLE).
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 177, column 2 (OFFER_TITLE).
Bulk load data conversion error (truncation) for row 181, column 20 (MANUFACTURER).
Bulk load data conversion error (truncation) for row 201, column 20 (MANUFACTURER).
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 202, column 2 (OFFER_TITLE).
Bulk load data conversion error (truncation) for row 261, column 2 (OFFER_TITLE).
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 265, column 2 (OFFER_TITLE).
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 299, column 2 (OFFER_TITLE).
Bulk load data conversion error (truncation) for row 316, column 2 (OFFER_TITLE).
Bulk load data conversion error (truncation) for row 327, column 21 (ATTRIBUTES).

You may check them all one by one and you will see where the problem is.
for example: Firs error in line 157, second column in the file has 105 characters and the column in the table has only 96
Now I cross-posted you.

As I said for testing bulk insert I was using first 100 rows as I was not testing data correctness.
The first problem was that the file had wrong new line character thus SQL server was not able to read it at all. but now as you can see you have problems in few lines. If this is one off import you may want just remove the few lines from the file and then process them separately.
Avatar of uma_

ASKER

Thanks a lot
Did it finally work for you?
Avatar of uma_

ASKER

Excellent!!!Thanks a lot