• 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 TestTable

    USE TestData
    GO
    CREATE TABLE CSVTest
    (ID INT,
    FirstName VARCHAR(40),
    LastName VARCHAR(40),
    BirthDate SMALLDATETIME)
    GO

    Create CSV file in drive C: with name sweetest. text with the following content. The location of the file is C:csvtest.txt

    1,James,Smith,19750101

    2,Meggie,Smith,19790122

    3,Robert,Smith,20071101

    4,Alex,Smith,20040202

    Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.

    BULK INSERT dbo.Z_tmp2
    FROM 'D: emp.csv'
    WITH
    (
    FIELDTERMINATOR = ','
      ,ROWTERMINATOR = ' '
      ,FIRSTROW=2
      --,DATAFILETYPE='widechar'
      ,CODEPAGE=65001
      --,ERRORFILE ='D:MDM_CICsourceerror.txt'    
      )
    GO

    --Check the content of the table.
    SELECT *
    FROM CSVTest
    GO
    --Drop the table to clean up database.
    DROP TABLE CSVTest
    GO

    Reference : Pinal Dave (http://blog.SQLAuthority.com)

  • 相关阅读:
    asp.net2.0系列视频教程
    Android入门教程(三十一)SQLite分页读取(转)
    sql语句大全
    判断是不是ie浏览器 加上ie11
    字节面试题
    泛型
    线程和
    sysbench工具
    自定义集合类
    类型通配符
  • 原文地址:https://www.cnblogs.com/ayforver/p/3945559.html
Copyright © 2020-2023  润新知