• [转]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 SELECTFROM clause within an INSERT statement. The basic syntax for bulk importing data is:
    
    INSERTSELECT * 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 	.
           Each new row is separated by new line char 
    .
    
    Players.fmt
    
    11.0
     3
     1       SQLCHAR             0       50      “	”   1     RK                           Latin1_General_CI_AI
     2       SQLCHAR             0       50      “	”   2     PLAYER                       Latin1_General_CI_AI
     3       SQLCHAR             0       50      “
    ”   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.
  • 相关阅读:
    Spring jdbcTemplat 写入BLOB数据为空
    Android强制弹出,隐藏输入法.
    Android NDK开发篇(五):Java与原生代码通信(数据操作)
    AIX加入�能够telnet远程连接账号方法
    index of rmvb mp3 rm突破站点入口下载
    C++“窗体”程序设计启蒙(之二)
    C++ Primer 学习笔记_72_面向对象编程 --句柄类与继承[续]
    (a*b)%c 小的技巧
    javascript(arguments)
    Cocos2dx 小技巧(十四)ScrollView实现缩放效果
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7880572.html
Copyright © 2020-2023  润新知