Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining column/field names from a csv file via SQL

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I'm looking to make use of data held within '.csv' files via SQL syntax.

However the fields held in these files can differ.

How can I obtain the list of column/field headers from a named 'csv' file via SQL syntax?

I'm currently using the following (for example) to return a result set - which has the column headers matching those as seen within the '.csv' file:

Code:
SELECT  * 
from OpenRowset('MSDASQL', 
  'Driver={Microsoft Text Driver (*.txt; *.csv)}; 
  DefaultDir=C:\Test', 
 'select * from File1.csv')

Any suggestions would be appreciated.

Thanks in advance.
 
If the csv file contains the column names, then it is the fisrt row that holds them. So if all you need is the column names, just do a select top 1 * from ....
 
That doesn't work. I have a '.csv' file with 11 records in it. The first row containing the column/field names.

When I run the stated code I get an sql result set of 10 records - with the result set field names matching those as found on the first row in the '.csv' file.

And it's that first row that I need.

Can something else be advised?
 
To confirm what I need...
Suppose I have the csv file which contains:

Code:
field1,field2,field3
one,two,three
four,five,six

The first row clearly holds the field names - 'field1', 'field2', 'field3'.

I want the SQL syntax that will return this as a result set.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top