一个简单的问题:
下面的更新查询在 SQL Server 中运行良好,但在 MySQL 中失败。
MySQL err.msg = "Error Code: 1093. You can't specify target table 'Pos' for update in FROM clause".
我可以找到几种解决方法,但寻找最佳实践。
update Pos set Printed = 1
where InvoiceNo = 3005
and Status = 'N'
and Pos.ItemNo IN
(select Pos.ItemNo from Pos,ItemMaster
where invoiceno = 3005
and status = 'N'
and printed = 0
and catType in ('B','L')
and Pos.itemno = ItemMaster.itemno)
最佳答案
这是我想出的“解决方法”。由于我是从 .NET 应用程序进行调用,因此我创建了一个存储过程来执行该作业。
DELIMITER //
CREATE PROCEDURE UpdatePrinted (IN varInvoiceNo VARCHAR(15))
BEGIN
DROP TEMPORARY TABLE IF EXISTS tmpParts;
CREATE TEMPORARY TABLE tmpParts (tmpItemNo VARCHAR(20) NOT NULL);
Insert Into tmpParts
select Pos.ItemNo from Pos,ItemMaster
where invoiceno = varInvoiceNo
and status = 'N'
and printed = 0
and catType in ('B','L')
and Pos.itemno = ItemMaster.itemno;
update Pos set Printed = 1 where InvoiceNo = varInvoiceNo and Status = 'N' and Pos.ItemNo IN (Select * from tmpParts);
END //
DELIMITER ;
关于mysql - SQL Server VS 带有子查询的 mySQL 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35185476/