28

I have several CSV files, which I want to import into an SQL Server database. I know if this is possible with BULK insert, but I want a solution, so that also the import table is automatically created before on basis the first row of the CSV files,are the column names.

Jagger
  • 10,350
  • 9
  • 51
  • 93
M. X
  • 1,307
  • 4
  • 19
  • 34
  • 1
    Do you need a program to do this? You can use SQL Server Management Studio import tool. – 03Usr Mar 27 '13 at 09:27

2 Answers2

33

SQL Server Management Studio provides an Import/Export wizard tool which have an option to automatically create tables.

You can access it by right clicking on the Database in Object Explorer and selecting Tasks->Import Data...

From there wizard should be self-explanatory and easy to navigate. You choose your CSV as source, desired destination, configure columns and run the package.

If you need detailed guidance, there are plenty of guides online, here is a nice one: http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

Nenad Zivkovic
  • 18,221
  • 6
  • 42
  • 55
  • 4
    This link does not cover CSV files, which was the question. – Ryan Apr 29 '13 at 18:10
  • 6
    @rncrtr I can assure you process is almost the same. There is a slight difference is some options after selecting flat-file as source instead of excel from the described, but wizard is good enough so it shouldn't be a problem to understand. – Nenad Zivkovic Apr 29 '13 at 20:45
  • There isn't a flat file option in the server I'm using, so this is somewhat of a dead end for me. I found out there is a separate tool that does contain it (Import and Export Data Tool), but it took a while to find out that was the ticket to use. Thanks. – Ryan Apr 29 '13 at 21:05
  • 1
    Open MS Excel, navigate to "Data" tab, "From Text" option. Make your ways through simple settings to import a csv file to your excel sheet. Save it as xls. Use the option mentioned in the answer above. Revel in the Magic! – Chiranjib Mar 08 '16 at 11:39
  • Wow, excellent answer. I went from googling around, to here, to having my data ready to query in SQL Server within just a few minutes. – Graham Apr 08 '16 at 19:38
  • @NenadZivkovic, wanna update the answer with the actual steps? – Mikael Dúi Bolinder Aug 22 '17 at 08:47
  • This did not work for me. "Error inserting data into table. (Microsoft.SqlServer.Import.Wizard) Object reference not set to an instance of an object. (Microsoft.SqlServer.Prose.Import)" – barlop Mar 29 '18 at 08:15
8

You can create a temp table variable and insert the data into it, then insert the data into your actual table by selecting it from the temp table.

 declare @TableVar table 
 (
    firstCol varchar(50) NOT NULL,
    secondCol varchar(50) NOT NULL
 )

BULK INSERT @TableVar FROM 'PathToCSVFile' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO

INSERT INTO dbo.ExistingTable
(
    firstCol,
    secondCol
)
SELECT firstCol,
       secondCol
FROM @TableVar

GO
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
03Usr
  • 3,335
  • 6
  • 37
  • 63