使用 bcp 将 csv 文件导入 sql 2005 或 2008 [英] Use bcp to import csv file to sql 2005 or 2008

查看:37
本文介绍了使用 bcp 将 csv 文件导入 sql 2005 或 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 csv 文件,我需要将它导入到 sql 2005 或 2008 中的表中.csv 中的列名和计数与表列名和计数不同.csv 被一个 ';' 分割.

I have a csv file and i need to import it to a table in sql 2005 or 2008. The column names and count in the csv are different from the table column names and count. The csv is splitted by a ';' .

示例

CSV 文件内容:

FirstName;LastName;Country;Age
Roger;Mouthout;Belgium;55

SQL 人员表

Columns: FName,LName,Country

推荐答案

我会创建一个临时表,批量插入批次,在新表中选择您需要的内容并删除临时表.

I'd create a temporary table, bulk insert the lot, select into the new table what you need and drop the temporary table.

类似的东西

CREATE TABLE dbo.TempImport
(
    FirstName varchar(255),
    LastName varchar(255),
    Country varchar(255),
    Age varchar(255)
)
GO
BULK INSERT dbo.TempImport FROM 'PathToMyTextFile' WITH (FIELDTERMINATOR = ';', ROWTERMINATOR = '
')
GO
INSERT INTO dbo.ExistingTable
(
    FName,
    LName,
    Country
)
SELECT  FirstName,
       LastName,
       Country
FROM       dbo.TempImport
GO
DROP TABLE dbo.TempImport
GO

这篇关于使用 bcp 将 csv 文件导入 sql 2005 或 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆