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:

INSERT … SELECT * FROM OPENROWSET(BULK…)

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
) ON [PRIMARY] 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. Players.fmt 11.0
3
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 Players.txt 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 www.directionstraining.com or call 1-855-575-8900.

[转]How to Import a Text File into SQL Server 2012的更多相关文章

  1. SQL Server 2012附加数据库时,错误提示如下:尝试打开或创建物理时,CREATE FILE 遇到操作系统错误 5(拒绝访问。)

    错误提示:Create File遇到操作系统错误5(拒绝访问) 解决方案: 在所有程序-SQL Server 2012-"SQL Server 配置管理器",点击"SQL ...

  2. SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server

    CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. Create TestTabl ...

  3. import data from excel to sql server

    https://www.c-sharpcorner.com/article/how-to-import-excel-data-in-sql-server-2014/ 需要注意的是,第一次是选择sour ...

  4. 6 ways to import data into SQL Server

    I’m going to go over some methods to import data from text files into SQL Server today. The particul ...

  5. 安装“Microsoft SQL Server 2014 Management Objects”时报错"Error Writing to file: Microsoft.SqlServer.XEvent.Linq.dll."

    问题: 当安装的软件依赖Microsoft SQL Server 2014 Management Objects时,会把这个组件打进安装包里,但是在服务器上安装时却报如下错误: “Error Writ ...

  6. create feature from text file

    '''---------------------------------------------------------------------------------- Tool Name: Cre ...

  7. shell脚本执行时报"bad interpreter: Text file busy"的解决方法

    在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...

  8. eclipse的使用-------Text File Encoding没有GBK选项的设置

    eclipse的使用-------Text File Encoding没有GBK选项的设置 2013-12-25 09:48:06 标签:java myeclipse使用 有一个项目是使用GBK编码的 ...

  9. Writing Text File From A Tabular Block In Oracle Forms

    The example given below for writing text file or CSV using Text_IO package from a tabular block in O ...

随机推荐

  1. [javascript]IIFE立即执行的函数表达式

    近况:最近一直忙着找实习没有更新,不过学习还是在继续的.最近在写Node.js又稍带把javascript的角落知识捡了一遍,过半个月打算去看看python和一些CSS深层的书和博客.工作找的还好,拿 ...

  2. dropdownlist的OnSelectedIndexChanged方法不触发

    解决方法: AutoPostBack="true" if (!IsPostBack){ 数据绑定 } 如果数据绑定后不触发,但是手动添加数据的情况下触发<%@ Page En ...

  3. Net Core SignalR 测试,可以用于unity、Layair、白鹭引擎、大数据分析平台等高可用消息实时通信器。

    SignalR介绍 SignalR介绍来源于微软文档,不过多解释.https://docs.microsoft.com/zh-cn/aspnet/core/signalr/introduction?v ...

  4. sp2010 升级sp2013 用户无法打开网站

    Add-PSSnapin microsoft.sharepoint.powershell $WebAppName = "http://wtcsps99:82/" $wa = get ...

  5. python使用venv环境报Python.h : No such file or direc

    这个是因为域名 install python36的时候没有按照python36-devel ,只需要 yum install python36-devel 然后重新安装所需模块即可.

  6. rocketmq学习

    官网地址 安装name server和broker git clone https://github.com/apache/incubator-rocketmq.git cd incubator-ro ...

  7. 读懂Netty的高性能架构之道

    Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...

  8. MyBatis Generator XML 配置

    使用反向生成器可以生成数据库表对应的实体类和mapper映射文件: 以下是具体介绍相应xml文件的配置: 附上一张配置的模板: <?xml version="1.0" enc ...

  9. js 从基础入门 到放弃 001

    快速入门  JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html> <head> <s ...

  10. 将一个List拆分为n份的方法

    public static void main(String[] args) { List<Integer> taskList = new ArrayList<>(); for ...