How to Import a Text File into SQL Server 2012

Importing a Text File into SQL Server 2012 using the OpenRowSet() Function

The OPENROWSET bulk row set provider is accessed by calling the OPENROWSET function and specifying the BULK option. The OPENROWSET(BULK…) function allows you to access remote data by connecting to a remote data source, such as a data file, through an OLE DB provider.

To bulk import data, call OPENROWSET(BULK…) from a SELECT…FROM clause within an INSERT statement. The basic syntax for bulk importing data is:


When used in an INSERT statement, OPENROWSET(BULK…) supports table hints. In addition to the regular table hints, such as TABLOCK, the BULK clause can accept the following specialized table hints: IGNORE_CONSTRAINTS (ignores only the CHECK constraints), IGNORE_TRIGGERS, KEEPDEFAULTS, and KEEPIDENTITY.

This example imports text file into SQL-Server 2012 using OpenRowSet command.

Step 1) Create a Data Table (corresponding to columns in text file)

CREATE TABLE [dbo].[players](
    [RK] [varchar](50) NULL,
    [PLAYER] [varchar](50) NULL,
    [Salary (US$)] [varchar](50) NULL

Step 2) Create a Format File Specific to Text File

  The file is using SQL Server edition 11.0 (i.e. SQL Server 2012)
      Table has 3 columns.
      Each column can accept 0 to 50 chars.
      Each column is tab separated \t.
      Each new row is separated by new line char \r\n.


1       SQLCHAR             0       50      “\t”   1     RK                           Latin1_General_CI_AI
2       SQLCHAR             0       50      “\t”   2     PLAYER                       Latin1_General_CI_AI
3       SQLCHAR             0       50      “\r\n”   3     Salary                Latin1_General_CI_AI

Step 3) Test OpenRowSet Command

Select document.* from openrowset(BULK N’C:\players.txt’,formatfile=N’c:\player.fmt’ ,firstrow=2) as document.

Step 4) Insert into Players Datatable

Insert into [dbo].[players]
select document.* from openrowset(BULK N’C:\players.txt’,formatfile=N’c:\players.fmt’,firstrow=2) as document


RK           PLAYER                 Salary
1         Alex Rodriguez         30,000,000
2        CC Sabathia                24,285,714
3        Mark Teixeira           23,125,000
4        Ichiro Suzuki            18,000,000
5        Derek Jeter               15,729,365
6        Mariano Rivera       14,940,025
7        Robinson Cano        14,000,000

Step 5) Verify Data in Players

Select * from dbo.players

For more Microsoft Technical Training information visit or call 1-855-575-8900.