你不能指定目标表' tbl_prod'用于FROM子句中的更新

时间:2014-10-06 01:49:58

标签: mysql vb.net

我想用另一个表中的当前数量+数量来更新prod_qty;

它在工作台中运行,SET SQL_SAFE_UPDATES = 0;但是当我尝试在VB.net中运行它时发生错误

  

您无法在FROM子句

中指定目标表'tbl_prod'以进行更新

这是查询

Update tbl_prod 
set prod_qty = prod_qty+
    (Select itp_needQty 
     from tbl_itmonpur 
     where itp_prodID = prod_id 
     and itp_status = 'PENDING' 
     and itp_InvoiceNo = 1004) 
where prod_id = 
    (Select itp_prodID 
     from tbl_itmonpur 
     where itp_prodID = prod_id 
     and itp_status = 'PENDING' 
     and itp_InvoiceNo = 1004);

我只是一名学生,所以请对我很轻松:)谢谢!

con.Open()
 qry = "UPDATE tbl_prod " + _
 "Join tbl_itmonpur " + _
 "ON tbl_itmonpur.itp_prodID = tbl_prod.prod_id " + _
 "SET tbl_prod.prod_qty = tbl_prod.prod_qty + tbl_itmonpur.itp_needQty " + _
 "WHERE tbl_itmonpur.itp_status = 'PENDING' AND tbl_itmonpur.itp_InvoiceNo @InvoiceNo; "
 cmd = New MySqlCommand(qry, con)
With cmd
    .Parameters.AddWithValue("@InvoiceNo", txtInvoicePend.Text)
End With
cmd.ExecuteNonQuery()
con.Close()

1 个答案:

答案 0 :(得分:0)

看起来所需的数量来自tbl_itmonpur表,其中itp_prodID列值与prod_id表的tbl_prod列匹配,因此您需要将两个表连接起来,如下所示< / p>

UPDATE tbl_prod 
JOIN tbl_itmonpur 
    ON tbl_itmonpur.itp_prodID = tbl_prod.prod_id
SET tbl_prod.prod_qty = tbl_prod.prod_qty + tbl_itmonpur.itp_needQty
WHERE tbl_itmonpur.itp_status = 'PENDING' 
AND tbl_itmonpur.itp_InvoiceNo = 1004;