连续插入错误数据
Bulk Insert with a wrong data in a row
我正在尝试为我的公司设置一个自动化数据工作流,每周一将数据插入数据库(Microsoft sql 服务器)。
"Bulk Insert"语句会逐行插入数据。但是,如果中间发现错误的数据,它会停止进程,不会取出插入的数据。
有什么方法可以让我们先验证数据,这样它才会开始插入,直到它验证数据是干净的才能插入吗?
谢谢!
在插入之前验证每一行是一种方法。
但是当你插入时仍然可能会出现错误。
所以,你可以考虑交易。整个插入在一个事务中。
任何行插入失败。数据库将回滚所有。
与交易。你之前不需要验证。
... if it finds wrong data in the middle, it will stop the process and it won't take out the data that were inserted.
使用事务,这正是它们的用途(作为事务的一部分回滚或提交多个操作)
General Remarks
The BULK INSERT statement can be executed within a user-defined transaction to import data into a table or view. Optionally, to use multiple matches for bulk importing data, a transaction can specify the BATCHSIZE clause in the BULK INSERT statement. If a multiple-batch transaction is rolled back, every batch that the transaction has sent to SQL Server is rolled back.
示例:
BEGIN TRANSACTION TrnBlkInsert
BEGIN TRY
-- your bulk insert here
BULK INSERT .....
COMMIT TRANSACTION TrnBlkInsert
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION TrnBlkInsert;
THROW;
END CATCH
我正在尝试为我的公司设置一个自动化数据工作流,每周一将数据插入数据库(Microsoft sql 服务器)。
"Bulk Insert"语句会逐行插入数据。但是,如果中间发现错误的数据,它会停止进程,不会取出插入的数据。
有什么方法可以让我们先验证数据,这样它才会开始插入,直到它验证数据是干净的才能插入吗?
谢谢!
在插入之前验证每一行是一种方法。 但是当你插入时仍然可能会出现错误。 所以,你可以考虑交易。整个插入在一个事务中。 任何行插入失败。数据库将回滚所有。 与交易。你之前不需要验证。
... if it finds wrong data in the middle, it will stop the process and it won't take out the data that were inserted.
使用事务,这正是它们的用途(作为事务的一部分回滚或提交多个操作)
General Remarks
The BULK INSERT statement can be executed within a user-defined transaction to import data into a table or view. Optionally, to use multiple matches for bulk importing data, a transaction can specify the BATCHSIZE clause in the BULK INSERT statement. If a multiple-batch transaction is rolled back, every batch that the transaction has sent to SQL Server is rolled back.
示例:
BEGIN TRANSACTION TrnBlkInsert
BEGIN TRY
-- your bulk insert here
BULK INSERT .....
COMMIT TRANSACTION TrnBlkInsert
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION TrnBlkInsert;
THROW;
END CATCH