ktaranov
10/19/2017 - 7:34 PM

BULK_INSERT_Demo.sql

USE master;
CREATE DATABASE [TestDB]
 CONTAINMENT = NONE
 ON  PRIMARY 
(  NAME = N'TestDB'
 , FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\TestDB.mdf'
 , SIZE = 4GB
 , MAXSIZE = UNLIMITED
 , FILEGROWTH = 4GB )
 LOG ON 
(  NAME = N'TestDB_log'
 , FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\TestDB_log.ldf'
 , SIZE = 1GB
 , MAXSIZE = 2048GB
 , FILEGROWTH = 1GB )
GO

-- https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/recovery-models-sql-server
ALTER DATABASE TestDB SET RECOVERY SIMPLE WITH NO_WAIT;
GO

USE TestDB;
GO

IF OBJECT_ID('dbo.TableTest', 'U') IS NOT NULL
DROP TABLE dbo.TableTest;
GO

CREATE TABLE dbo.TableTest(
  ActionDate  datetime2(0),
  ActionValue decimal(12,3),
  IsFlag      bit
);
GO

-- https://docs.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql
BULK INSERT TableTest
FROM 'D:\MyBigFile.csv'
WITH  
( CODEPAGE = '65001',  
  DATAFILETYPE = 'char',  
  FIELDTERMINATOR = ',' ,
  FIRSTROW = 2
);
GO

/*
MyBigFile.csv file content:
ActionDate,ActionValue,IsFlag
2017-10-19,1.000123,1
2017-10-19,2.100123,0
2017-10-19,3.000123,1
*/