Tuesday, June 14, 2016

SQL 2014 - How to import CSV files into a table on the fly

Check your Administrative tools>ODBC 64>Drivers and see if you have a Microsoft Access Text Driver.
This will exist if you've installed access or the database components

--Run these to enable the options

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO

--Run one of these to import your file, depending on which driver you have installed

SELECT * into MyTable FROM OPENROWSET ('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=C:\FilePath;', 'SELECT * from [MyFile.csv]')

--or

SELECT * into MyTable FROM OPENROWSET ('MSDASQL', 'Driver={Microsoft Access Text Driver (*.txt, *.csv)};DBQ=C:\Filepath', 'SELECT * from [MyFile.csv]')

No comments:

Post a Comment