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. 如何将Jenkins multiline string parameter的多行文本优雅的保存为文件

    [现象]: 使用multi-line string parameter获取的文本变量,在jenkins shell里面显示为单行文本(空格分割). [问题]:能否转换为多行文本,并存入文件. [解决方 ...

  2. Day 29 _模块二 -hashlib_configparse_logging

    一.hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数 ...

  3. 文件操作(FILE)与常用文件操作函数

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

  4. Android 美学设计基础 <2>

    我们接着上期的项目进行分享. 1.2.2 Elevation (仰角) 仰角只的是不同的素材块在Z轴上的选对位置 仰角的测量方式 这里只的是两个平面在z的距离,记住我们默认每个平面都是1dp的厚度 1 ...

  5. nodejs之模块加载机制

    nodejs模块加载原理 node加载模块步骤: 1) 路径分析 (如判断是不是核心模块.是绝对路径还是相对路径等) 2) 文件定位 (文件扩展名分析, 目录和包处理等细节) 3) 编译执行 原生模块 ...

  6. iOS多线程---GCD中线程的通信

    在子线程的任务完成后,有时候需要从子线程回到主线程,刷新UI. 从子线程中回到主线程,以前已经写过一种方法: [self.imageView performSelectorOnMainThread:@ ...

  7. Linux必备命令

      目录                                                              概述 常用系统工作命令 系统状态检测命令 工作目录切换命令 文本文件 ...

  8. uiautomatorviewer 双击闪退问题解决

    最近在学习app自动测试,结果在打开uiautomatorviewer查看app界面元素时,就出现了闪退的问题,找了很多很多方法,最后终于可以解决了,详情请继续往下看 首次安装adt的步骤 将下载的压 ...

  9. TapTap推广统计逻辑

    当我们在Taptap上访问某款游戏时,比如https://www.taptap.com/app/34762,taptap会记录下这次访问,它是怎么做的呢. 首先,用记事本打开这个网址,在head部分看 ...

  10. DataTable自身查询方法

    这里说到的查询有两种. 1.DataTable.Select 2.DataTable.Rows.Find   a>先建立一个DataTable供使用吧. /// <summary> ...