Link to home
Create AccountLog in
Avatar of ShreyaTrivedi
ShreyaTrivediFlag for India

asked on

Sql Query for reading a comma delimited textfile

I want Sql query which can read a comma-delimited text file and create a table for it. I know that it can be done using loop by reading line by line. But I want a query which can directly do the task. I had found that query which can directly read a delimited textfile and create a table for it. But it has been misplaced. That was a direct sql query which also created a table alongwith reading the file. I want that query.
ASKER CERTIFIED SOLUTION
Avatar of rachitkohli
rachitkohli
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ShreyaTrivedi

ASKER

How can I assign delimiter to the query? What if I have comma delimited file?
Hi,
try this:

INSERT INTO TableName
SELECT *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Text;Database=C:\TEST\;',
   'SELECT * FROM test1.csv')
The following query works-
bulk insert txtTable from 'c:\test\test.txt' with (FieldTerminator=',')