SQL Server BULK INSERT

Summary: in this tutorial, you’ll learn how to use the SQL Server BULK INSERT statement to import a data file into a database table.

Introduction to the SQL Server BULK INSERT statement

The BULK INSERT statement allows you to import a data file into a table or view in SQL Server. The following shows the basic syntax of the BULK INSERT statement:

BULK INSERT table_name
FROM path_to_file
WITH options;Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, specify the name of the table in the BULK INSERT clause. Note that you can use the fully qualified table name such as database_name.schema_name.table_name.
  • Second, provide the path to the file in the FROM clause.
  • Third, use one or more options after the WITH keyword.

For the detail of the BULK INSERT statement, you can read it here.

参考文档来源如下

BULK INSERT (Transact-SQL) - SQL Server | Microsoft Learn

SQL Server BULK INSERT statement example

使用BULK INSERT statement加载本地CSV文件到表中.

创建HRDB

CREATE DATABASE HRDB
GO

创建表Employees 

USE HRDB
GO

CREATE TABLE Employees (
  Id int IDENTITY PRIMARY KEY,
  FirstName varchar(50) NOT NULL,
  LastName varchar(50) NOT NULL
)
GO

加载文件 D:\data\employees.csv

Id,First name,Last name
1,John,Doe
2,Jane,Doe
3,Lily,Bush
4,SQLServer,BULK INSERT
5,ALwayson,HRDB

into the Employees table

BULK INSERT Employees
FROM 'D:\data\employees.csv'
WITH (
  FIELDTERMINATOR = ',',
  ROWTERMINATOR = '\n',
  FIRSTROW = 2
);

query the data from the Employees table

SELECT * FROM employees;
Id          FirstName                                          LastName
----------- -------------------------            ----------------------------
1           John                                               Doe
2           Jane                                               Doe
3           Lily                                               Bush
4           SQLServer                                          BULK INSERT
5           ALwayson                                           HRDB

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-15 15:38:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-15 15:38:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-15 15:38:05       87 阅读
  4. Python语言-面向对象

    2024-05-15 15:38:05       96 阅读

热门阅读

  1. 常见网络攻击及解决方案

    2024-05-15 15:38:05       34 阅读
  2. 2024年广东省助理工程师职称评审指南!

    2024-05-15 15:38:05       39 阅读
  3. 【数据库】银行转账并发问题

    2024-05-15 15:38:05       35 阅读
  4. 学习MySQL(一):了解数据库

    2024-05-15 15:38:05       34 阅读
  5. Flink中基于Chandy-Lamport算法的分布式快照实现详解

    2024-05-15 15:38:05       37 阅读
  6. Qt中的TCP通信:一个详细指南

    2024-05-15 15:38:05       35 阅读
  7. LeetCode1523.在区间范围内统计奇数数目

    2024-05-15 15:38:05       36 阅读
  8. 桥接模式

    2024-05-15 15:38:05       31 阅读