我在这里得到了一个大小合适的清理查询,只有15行左右的DELETE
语句。我注意到由于FK引用约束,最后几行有时会失败,但是一旦我重新运行查询,剩余的数据就会被删除。我的研究让我相信这是一个时间问题。但是,我无法在每个语句后添加GO
,因为我的变量会失去范围......
DECLARE @userOrders TABLE(OrderId INT)
DECLARE @userInventoryCatalog TABLE(InventoryId INT)
DECLARE @userSpotCheckRequests TABLE(SpotCheckRequestId INT)
INSERT INTO @userOrders
SELECT OrderId FROM [Order] WHERE UserId = @userId
INSERT INTO @userInventoryCatalog
SELECT InventoryId FROM [Inventory] WHERE UserId = @userId
INSERT INTO @userInventoryCatalog
SELECT SpotCheckRequestId FROM SpotCheckRequest WHERE InventoryId IN (SELECT
* FROM @userInventoryCatalog)
DELETE FROM ClientCustomerNotes WHERE ClientCustomerId IN (SELECT
ClientCustomerId FROM ClientCustomers WHERE UserId = @userId)
DELETE FROM InventoryShelveMapping WHERE InventoryId IN (SELECT * FROM
@userInventoryCatalog)
DELETE FROM UserBinAllocationLog WHERE UserId = @userId
DELETE FROM OrderMantoxTeaInventory WHERE MantoxTeaInventoryId IN (SELECT *
FROM @userInventoryCatalog)
DELETE FROM InventoryLog WHERE InventoryId IN (SELECT * FROM
@userInventoryCatalog)
DELETE FROM Analytics WHERE UserId = @userId
DELETE FROM SpotCheckRequestItems WHERE SpotCheckRequestId IN (SELECT * FROM
@userSpotCheckRequests)
DELETE FROM SpotCheckRequest WHERE InventoryId IN (SELECT * FROM
@userInventoryCatalog)
DELETE FROM UserNonReturnableItems WHERE InventoryId IN (SELECT * FROM
@userInventoryCatalog)
DELETE FROM InventoryBundleDefinition WHERE InventoryId IN (SELECT * FROM
@userInventoryCatalog)
DELETE FROM Inventory WHERE UserId = @userId
DELETE FROM [Log] WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderTracking WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderProperties WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderDimensions WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderAttachments WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderPicker WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderZenDesk WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderReprocessingQueue WHERE OrderId IN (SELECT * FROM
@userOrders)
DELETE FROM OrderPicker WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM OrderInformation WHERE OrderId IN (SELECT * FROM @userOrders)
DELETE FROM [Notification] WHERE UserId = @userId
DELETE FROM [TransactionPaymentLog] WHERE UserId = @userId
DELETE FROM [TransactionLog] WHERE UserId = @userId
DELETE FROM [Order] WHERE UserId = @userId
DELETE FROM UserContacts WHERE UserId = @userId
DELETE FROM ClientCustomers WHERE UserId = @userId
所以我的问题是:
如何确保多个语句同步运行?
答案 0 :(得分:0)
更改isolation level
并设置事务并执行delete
语句。当您处理删除时,它将确保不更新值,以便delete
同步发生。
set transaction isolation level repeatable read
BEGIN transaction
DELETE Statements....
COMMIT transaction
您还可以阅读more about isolation levels。