存储表中多行的值

时间:2016-02-18 23:02:41

标签: sql stored-procedures sql-server-2012

我有一个每天运行的存储过程来为我们的业务提取库存号。我被要求更新存储过程以获取已在部件上写入采购订单的某些行的信息。例如

Tbl A(本地服务器)

partnumber    poNumber  ReceivedDate   PartStatus

Tbl B(链接服务器)

poNumber      partnumber  ReceivedDate

我只需查看TblA.PartStatus = "Shipped" AND tblA.poNumber = tblB.poNumber的项目,然后查看TblB.ReceivedDate是否为空。

如果TblB.ReceivedDate不为空,我需要设置TblA.ReceivedDate = TblB.ReceivedDate

如何将其设置为遍历表格或执行单个连接语句以使其更快?

1 个答案:

答案 0 :(得分:0)

一种方法是:

UPDATE a SET a.ReceivedDate = b.ReceivedDate
FROM tblA AS a
    INNER JOIN tblB AS b 
        ON (a.poNumber = b.poNumber AND a.partNumber = b.partNumber AND a.partStatus = 'Shipped' AND b.ReceivedDate IS NOT NULL)