【问题标题】:SQL Server VS mySQL Update with Sub Queries带有子查询的 SQL Server VS mySQL 更新
【发布时间】:2016-05-13 03:32:15
【问题描述】:

一个简单的问题:

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

【问题讨论】:

标签: mysql sql-server


【解决方案1】:

这是我想出的“解决方法”。由于我是从 .NET 应用程序进行调用,因此我创建了一个执行 JOB 的存储过程。

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 ;

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2012-11-21
    相关资源
    最近更新 更多