SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
2014-08-29 18:04
686 查看
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable
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.
Reference : Pinal Dave (http://blog.SQLAuthority.com)
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:\temp.csv' WITH ( FIELDTERMINATOR = ',' ,ROWTERMINATOR = '\n' ,FIRSTROW=2 --,DATAFILETYPE='widechar' ,CODEPAGE=65001 --,ERRORFILE ='D:\MDM_CIC\source\error.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)
相关文章推荐
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- How do I load text or csv file data into SQL Server?
- [SQL Server][FILESTREAM] -- Using INSERT, UPDATE and DELETE to manage SQL Server FILESTREAM Data
- C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net
- ?? [SQL Server] -- Using FILESTREAM to Store BLOBs in the NTFS File System in SQL Server 2008
- Bulk Insert from Flat File Using Transact-SQL
- Import/Export Excel (.Xlsx) or (.Xls) File into SQL Server
- using sqlbulkcopy to quick load data from your client to sqlserver
- Import data from files to SQL Server(用BULK INSERT命令导入数据详解)
- [转]How to Import a Text File into SQL Server 2012
- [SQL Server][FILESTREAM] -- Best Practices When Using FILESTREAM Feature of SQL Server 2008
- How To Load CLOB Data from a File into a CLOB column using PL/SQL
- Import XML file into SQL Server
- Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert )
- How to import a SQL Server .bak file into MySQL?
- SQL Server Bulk Insert 只需要部分字段时的方法
- VBS Get Sql Server Table Data and Export to CSV File...
- SQL Server里面如何导出包含(insert into)数据的SQL脚本
- VBS Get Sql Server Table Data and Export to CSV File...
- How to import LDIF file into LDAP server with command line