更新并设置“连接表”和“派生”表

时间:2018-05-15 04:16:36

标签: sql-server

我想更新AddressL1,AddressL2和我的SalesOrderNumber的城市:

To:AddressL1:4 Pullum St. 地址L2:Birkenhead 城市:奥克兰

我的代码是:

SELECT
    TOP (200) dbo.SalesOrder.SalesOrderID, dbo.SalesOrder.SalesOrderNo,
    dbo.SalesOrder.CustomerID, dbo.qryAddress.AddressL1,
    dbo.qryAddress.AddressL2, dbo.qryAddress.City
FROM
    dbo.SalesOrder
    LEFT OUTER JOIN dbo.qryAddress ON dbo.SalesOrder.AddressID = dbo.qryAddress.AddressID
WHERE
    (dbo.SalesOrder.SalesOrderNo = 'so18050653')

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试以下查询

 UPDATE A 
    SET A.AdddressL1 = '4 Pullum St',
       A.Addressl2 = 'Birkenhead',
       A.City='Auckland' 
    FROM dbo.qryAddress A
    INNER JOIN dbo.SalesOrder S ON A.AddressID = S.AddressID
WHERE S.SalesOrderNo = 'so18050653'

答案 1 :(得分:0)

要更新数据库中的数据,请使用UPDATE命令。好的参考here和子查询看起来here。对于您的情况,您需要在WHERE子句上使用子选择命令来获取您拥有的SalesOrderNo键的AddressID。请注意,这将更新与SalesOrderNo关联的任何地址。首先运行subselect以查看它返回的内容可能是个好主意。它应该返回一个AddressID。

UPDATE qryAddress a
SET AdddressL1 = '4 Pullum St',
   Addressl2 = 'Birkenhead',
   City='Auckland'
WHERE a.AddressID IN (SELECT s.addressID FROM SalesOrder s WHERE SalesOrderNo='so18050653')