Different ways to Import Data into SQL Server.

In This article I am going to explain different ways to import data into SQL Server, excluding import / Export wizards or SSIS. There are so many ways to do so I will explain some of them.


 

BCP

This is most commonly used option. This allows both import and export data, but it is primarily used for text data formats. The BCP utility can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files. This is generally run from a Windows command prompt, but could also be called from a stored procedure by using xp_cmdshell or called from a SSIS package.

Here is a simple command for importing data from file C:\ImportData.txt into table dbo.ImportTest.

bcp dbo.ImportTest in ‘C:\ImportData.txt’ -T -SserverName\instanceName

 

BULK INSERT

This command is a T-SQL command that allows you to import a data file into a database table or view in a user-specified format.

BULK INSERT enforces strict data validation and data checks of data read from a file that could cause existing scripts to fail when they are executed on invalid data. For example, BULK INSERT verifies that:

  • The native representations of float or real data types are valid.
  • Unicode data has an even-byte length.

This command imports data from file C:\ImportData.txt into table dbo.ImportTest.

BULK INSERT dbo.ImportTest
FROM ‘C:\ImportData.txt’
WITH ( FIELDTERMINATOR =’,’, FIRSTROW = 2 )

 

OPENROWSET

This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server.  This command is an alternative to accessing tables in a linked server and is a one-time, ad hoc method of connecting and accessing remote data by using OLE DB.

The OPENROWSET function can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement, subject to the capabilities of the OLE DB provider. Although the query might return multiple result sets, OPENROWSET returns only the first one.

By using this command along with an INSERT INTO command we can load data from the specified data source into a SQL Server table.

This command will pull in all data from worksheet [Sheet1$]. By using the INSERT INTO command you can insert the query results into table dbo.ImportTest.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;Database=C:\ImportData.xls’, [Sheet1$])

Here is another example where data is pulled from worksheet [Sheet1$] by using a SELECT * FROM command. Again, by using the INSERT INTO command you can insert the query results into table dbo.ImportTest.   The query can be any valid SQL query, so you can filter the columns and rows by using this option.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;Database=C:\ImportData.xls’, ‘SELECT * FROM [Sheet1$]’)

 

OPENDATASOURCE

This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server. This is similar to the OPENROWSET command.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENDATASOURCE(‘Microsoft.Jet.OLEDB.4.0’,
‘Data Source=C:\ImportData.xls;Extended Properties=Excel 8.0’)…[Sheet1$]

OPENQUERY

Another option is OPENQUERY.  This is another command that allows you to issue a T-SQL command to select data and again with the INSERT INTO option we can load data into our table.  There are two steps with this process, first a linked server is setup and then second the query is issued using the OPENQUERY command.  This option allow you to filter the columns and rows by the query that is issued against your linked data source.

EXEC sp_addlinkedserver ‘ImportData’,
   ‘Jet 4.0’, ‘Microsoft.Jet.OLEDB.4.0’,
   ‘C:\ImportData.xls’,
   NULL,
   ‘Excel 8.0’
GO

INSERT INTO dbo.ImportTest
SELECT *
FROM OPENQUERY(ImportData, ‘SELECT * FROM [Sheet1$]’)


 

Linked Servers

Here is yet another option with setting up a linked server and then issuing a straight SQL statement against the linked server.  This again has two steps, first the linked server is setup and secondly a SQL command is issued against the linked data source.

EXEC sp_addlinkedserver ‘ImportData’,
   ‘Jet 4.0’, ‘Microsoft.Jet.OLEDB.4.0’,
   ‘C:\ImportData.xls’,
   NULL,
   ‘Excel 8.0’
GO

INSERT INTO dbo.ImportTest
SELECT * FROM ImportData…Sheet1$

 

Source: http://www.mssqltips.com/sqlservertip/1207/different-options-for-importing-data-into-sql-server/?utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=20130522

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s