mysql - SQL Server VS 带有子查询的 mySQL 更新

标签 mysql sql-server

一个简单的问题:

下面的更新查询在 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/

相关文章:

c++ - 在 C++ 中序列化对象并在 mysql 中存储为 blob 类型

php - 使用左连接时的Mysqli转换

sql-server - SQL Server Management Studio 18 日志文件?

sql-server - SQL Server Express 安装失败

sql-server - 使用 SQL Server 在 varchar 列中查找非 ASCII 字符

sql - mysql:平均一行中的多列,忽略空值

mysql - 如何计算两组表的不同之处

MySQL 计算重复行

sql - 将表转换为 SQL Server 2016 中每列值的 JSON

c# - C#中恢复数据库的问题