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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Insert from a txt file

Author  Topic 

taunt
Posting Yak Master

128 Posts

Posted - 2014-06-09 : 17:49:32
Hello I'm running this:

INSERT INTO update
SELECT * FROM 'C:\update.txt'


and I get this:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'C:\update.txt'.


The file is comma separated and " is the text qualifier. How would I go about the query so I can import from the text file?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-09 : 18:07:49
You can use BULK INSERT for this.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2014-06-10 : 01:45:40
Something like:
BULK

INSERT mytable

FROM 'h:\sqlserver-dba-csv.txt'

WITH

(

FIELDTERMINATOR = ',',

ROWTERMINATOR = '\n'

)

--ref:
http://www.sqlserver-dba.com/2011/05/bulk-insert-csv-into-a-sql-server-table.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

taunt
Posting Yak Master

128 Posts

Posted - 2014-06-10 : 11:43:05
OK I tried this:
set ANSI_WARNINGS  Off
GO
BULK INSERT update
FROM 'C:\update.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
set ANSI_WARNINGS On
GO



and got this:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'C:\update.txt'.
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 5, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 6, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 7, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 8, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 9, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 10, column 5 (CatalogNumber).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 11, column 5 (CatalogNumber).
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)".


quote:
Originally posted by jackv

Something like:
BULK

INSERT mytable

FROM 'h:\sqlserver-dba-csv.txt'

WITH

(

FIELDTERMINATOR = ',',

ROWTERMINATOR = '\n'

)

--ref:
http://www.sqlserver-dba.com/2011/05/bulk-insert-csv-into-a-sql-server-table.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-10 : 11:53:57
Try putting square brackets around your table name since you are using a reserved word.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-10 : 11:54:22
Is the SQL Server instance running locally? B ULK INSERT runs from the perspective of the database server and not your client machine.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -