Pages

Wednesday, September 5, 2012

Openrowset to pull data from CSV to SQL Server

Openrowset to pull data from CSV to SQL Server:


Openrowset is a function which can be referenced after the "FROM" Clause of T-SQL.

This function is used to pull data from different databases by utlising the ODBC drivers installed in the particular windows system.

How to Pull the data from other SQL Server :


Syntax:

Below is the syntax for pulling the data from path \\share\file.csv and read that in SQL Server query analyser.

SELECT * FROM OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)}','SELECT * FROM \\share\file.csv')

Load the data pulled from CSV or text file into SQL Server Table. 

SELECT * into dbo.newtablename FROM OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)}','SELECT * FROM \\share\file.csv')

Hope the above information helps

Thanks