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. AbpZero之企业微信---登录(拓展第三方auth授权登录)---第三步:需要注意事项

    1.AbpZero的auth登录会在数据库中的AbpUserLogins表会根据你登录的ProviderKey和Provider来生成生成一条记录,ProviderKey在表中是唯一的: 2.要登录成 ...

  2. CS中窗体的基类(BaseForm)注意点

    窗体基类最好新建一个窗体(BaseForm) 1.这样能够保证在VS中保证他的派生窗口也能够可视化. 2.如果基类直接是一个cs类文件,对于处理派生窗口就很复杂,比如按钮权限之类的操作; 如果直接继承 ...

  3. Asp.Net Web Api中使用Swagger

    关于swagger 设计是API开发的基础.Swagger使API设计变得轻而易举,为开发人员.架构师和产品所有者提供了易于使用的工具. 官方网址:https://swagger.io/solutio ...

  4. .net core webapi+EF Core

    .net core webapi+EF Core 一.描述: EF Core必须下载.net core2.0版本 Micorsoft.EntityFrameworkCore:EF框架的核心包Micor ...

  5. JQuery Mobile - 为什么绑定事件后会被多次执行?

    JQuery Mobile 在绑定事件时候,发现会被多次执行,为什么啊? 原来,jquery click  不是替换原有的function ,而是接着添加,所以才会执行次数越来越多,怎么办才能按需实现 ...

  6. list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步

    1.list页面:筛选功能优化,显示choices,传condition过滤筛选项 list_filter = [     # sites.FilterOption("name", ...

  7. 操作实践题 - HTML 列表综合应用

    通过对列表的综合应用,编写如下效果网页: 解答: <html> <head> <title>操作实践题</title> <meta http-eq ...

  8. POST 400 (BAD REQUEST)

    遇到这种错,1,F12打开控制台,2,点击network,找到发送的请求. 3,点击请求,4,看右侧的 Header Preview Response 应该能找到原因. 就拿刚才来说,找了好几个小时原 ...

  9. 使用 find 命令实现高级排除需求

    使用 find 命令实现高级排除需求 Linked 关于 find 命令,本博客介绍过 atime,ctime,mtime 介绍过 --exec 参数. 介绍这些的基本需求是进行文件管理.事实上,基于 ...

  10. P1494 小Z的袜子

    P1494 小Z的袜子 莫队板子题,对询问进行排序+分块,从而得到巧妙的复杂度 对于L,R的询问. 设其中颜色为x,y,z的袜子的个数为a,b,c... 那么答案即为 (a*(a-1)/2+b*(b- ...