3

I am using Microsoft SQL Server Management studio and I am currently importing some CSV files in a database. I am importing the CSV files using the BULK INSERT command into already existing tables, using the following query.

BULK INSERT  myTable
    FROM >>'D:\myfolder\file.csv'
    WITH
    (FIRSTROW = 2,
    FIELDTERMINATOR = ';', --CSV Field Delimiter
    ROWTERMINATOR = '\n', -- Used to shift to the next row
    ERRORFILE = 'D:\myfolder\Error Files\myErrrorFile.csv',
    TABLOCK
    )

This works fine for me thus far, but I would like to automate the process of naming columns in tables. More specifically I would like to create a table and use as column names, the contents of the first row of the CSV file. Is that possible?

Nisfa
  • 359
  • 1
  • 4
  • 16
  • this may help https://stackoverflow.com/questions/2381528/how-to-bulk-insert-a-file-into-a-temporary-table-where-the-filename-is-a-varia but is https://learn.microsoft.com/en-us/sql/integration-services/sql-server-integration-services and https://learn.microsoft.com/en-us/sql/ssdt/download-sql-server-data-tools-ssdt more useful for this job. – Steve Drake Dec 01 '17 at 09:55

1 Answers1

0

The easiest way I can think of is:

right-click on the database, select: Tasks -> Import Data...

After that, SQL Server Import and Export Wizard will display. There you have everything to specify and custom settings on importing data from any sources (such as getting column names from first row in a file).

In your case, your data source will be Flat file source.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • First of all thanks for the answer! What I actually want is to implement it using a query. This is a requirement that has been set and it is something that would enable me to do the whole process faster. Do you have any idea on how to construct such a query? – Nisfa Dec 01 '17 at 12:07