0

I have this query:

BULK INSERT Employee
FROM 'E:\\file.txt' --location with filename
WITH
(
   FIELDTERMINATOR = ' ',
   ROWTERMINATOR = '\n'
)
GO

And in the my file, the data looks like this:

43266200 6827           43295200 1393/05/23 14:26:26     18      1

I want insert this data into my table, but in file in the my fieldterminator have a error and SQL Server complains about space or tab between fields error. How can I solve this?
can i use this?

BULK INSERT Employee
    FROM 'E:\\file.txt' --location with filename
    WITH
    (
       FIELDTERMINATOR = ' ' or '  ' or '        ',
       ROWTERMINATOR = '\n'
    )
    GO


or how can i import this txt file into sql server?

2
  • FROM 'E:\\file.txt' Is this the actual SQL you're running or are you trying to extract it from your C# source code, sans "? Because it looks like the latter. In which case, pay attention to the actual SQL statement being run. Commented Nov 1, 2014 at 6:27
  • i think between the field in my file has a tab space and simple space for example 43266200space6827\t43295200 Commented Nov 1, 2014 at 6:30

2 Answers 2

4

If you have a tab between your fields in the data file, then use this:

BULK INSERT Employee
FROM 'E:\\file.txt' --location with filename
WITH
(
   FIELDTERMINATOR = '\t',
   ROWTERMINATOR = '\n'
)
GO

The \t denotes a tab - that should tell SQL Server to interpret a tab as a separator between two fields.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for attention to my problem,but my friend in the my file for example between the 43266200 and 6827 field have a 3 space and between 6827 and 43295200 have a \t
and when your query run in my sql server:
Msg 4832, Level 16, State 1, Line 1 Bulk load: An unexpected end of file was encountered in the data file. 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)". error
You need to clean your file before importing - you cannot have three spaces in one place, and a tab in another. This has to be consistent throughout the file!
but my file has 27000000 record! how can i clean record in big txt file?
|
0

Code:

BULK INSERT #ProspectiveFile
                  FROM ''C:\Matt\' + @DataFileName + '.txt''
                  WITH (ROWTERMINATOR = ''\n''
                       ,FIELDTERMINATOR = ''    '')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.