简体   繁体   English

是否应在TVP中插入行?

[英]Should inserting rows with TVP be in transaction?

I have a TVP type List_Of_Items, and want to insert multiple rows at once, so my choice is to use stored procedure with TVP parameter like this below. 我有一个TVP类型List_Of_Items,并且想要一次插入多行,因此我的选择是将存储过程与TVP参数一起使用,如下所示。 Is such a inserting safe and I can get rid of the transaction? 这样的插入安全吗,我可以摆脱交易了吗? Or its potenially dangerous as the insert can break in the middle so transaction is must be? 还是它的潜在危险,因为插入内容可能会在中间破裂,因此必须进行交易吗?

CREATE PROC Insert_Order_With_Details 
(
     @Items List_Of_Items
) 
AS
BEGIN
    BEGIN TRANSACTION
        INSERT INTO OrderDetails (OrderId, CustomerId, ItemId, Quantity)
            SELECT @OrderID, @CustomerID, ItemID, Quantity
            FROM @Items
    COMMIT
END

No, you should only use explicit transactions when you have multiple SQL statements that you want to run as an atomic operation. 不,只有当您有多个要作为原子操作运行的SQL语句时,才应使用显式事务。

Single SQL statements run in an implicit transaction, so any single statement can either complete successfully or fail entirely - you will never have an insert...select that fails in the middle of the select operation and inserts only a part of the rows. 单个SQL语句在隐式事务中运行,因此任何单个语句都可以成功完成或完全失败-您将永远不会有在select操作中间失败的insert...select且仅插入部分行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM