1

I am using BULK INSERT to read a csv file in SQL Server. Is there a way to keep the first row as column name while I am reading the file?

If not, after reading the data from the csv (from second row), how can I add column names to it?

Any help/comments/suggestions are much appreciated.

0

If not, after reading the data from the csv (from second row), how can I add column names to it?

I would put your focus here. First, create your table with yoru column names, data types, etc.

create table myTable (column1 <datatype>, column2 <datatype>)

Then, bulk insert into it but ignore the first row.

bulk insert myTable
   from 'C:\somefile.csv',
   with( firstrow = 2,
         fieldterminator = ',',
         rowterminator = '\n')

If it's dynamic, then you may want to look into openrowset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.