如何在BULK INSERT期间修改.csv中的数据?

时间:2016-02-26 22:14:45

标签: sql-server csv bulkinsert

我试图将使用MySQL构建的Web应用程序转换为Microsoft SQL并需要一些指导。我有各种不同的CSV数据来源,我使用LOAD DATA LOCAL INFILE修改内容(例如将大小写更改为大写,删除空格,将多个字段连接成一个等),添加一些数据(帐号和当前日期/时间),忽略一些数据(分配给虚拟变量,永不使用),并将数据放入我的数据库中正确的列。我可以使用MSSQL获得相同的结果吗?

以下是MySQL版本的示例导入代码段:

LOAD DATA LOCAL INFILE 'testDataFile.csv'
INTO TABLE tbl_raw_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(
@order_date,
@order_number,
@site_name,
@patient_name_last,
@patient_name_first,
@dummy,
@dummy, 
@dummy,
@medication_ndc_prefix,
@dummy,
@dummy,
@patient_ID_number,
@prescriber_ID, 
@order_retail,  
@insurance_ID,
@dummy,
@order_reimbursement,
@dummy,
@dummy,
@dummy, 
@dummy,
@dummy,
@order_acquisition_cost,    
@dummy,
@dummy,
@dummy
)
SET
order_number = UPPER(TRIM(@order_number)),
site_name = UPPER(TRIM(@site_name)),
patient_name_last = UPPER(TRIM(@patient_name_last)),
patient_name_first = UPPER(TRIM(@patient_name_first)),
patient_ID_number = UPPER(TRIM(@patient_ID_number)),
prescriber_ID = UPPER(TRIM(@prescriber_ID)),
insurance_ID = UPPER(TRIM(@insurance_ID)),
order_date = str_to_date(@order_date, '%m/%d/%Y'),
order_retail = REPLACE(@order_retail,'$',''),
order_reimbursement = REPLACE(@order_reimbursement,'$',''),
order_acquisition_cost = REPLACE(@order_acquisition_cost,'$',''),
medication_ndc_prefix = LEFT(REPLACE(@medication_ndc_prefix, '-', ''),9),
patient_ID = CONCAT(TRIM(patient_name_last),',',trim(patient_name_first),'-',patient_ID_number),
order_added_on = CURRENT_TIMESTAMP,
account_ID = 1

如果重要的话,Web界面是用PHP构建的。

1 个答案:

答案 0 :(得分:0)

使用OPENROWSET

INSERT INTO dbo.YourTable
SELECT a.* FROM OPENROWSET( BULK 'D:\our.csv', FORMATFILE = 'D:\our.fmt') AS a;

our.fmt的样本(它描述了csv中的字段的文件)

9.0
4
1  SQLCHAR  0  50 ";"        1  Field1                SQL_Latin1_General_Cp437_BIN
2  SQLCHAR  0  50 ";"        2  Field2                SQL_Latin1_General_Cp437_BIN
3  SQLCHAR  0  50 ";"        3  Field3                SQL_Latin1_General_Cp437_BIN
4  SQLCHAR  0  500 "\r\n"      4  Field4        SQL_Latin1_General_Cp437_BIN

您可以找到*.fmt here的说明。