从选择错误的SQL更新

时间:2013-08-20 20:07:56

标签: sql sql-server sql-update

Update PSSFAAssets

Set PSSFAAssets.ProjectID = xhh_quickscan.ProjectID,
    PSSFAAssets.CpnyAssetNo = xhh_quickscan.Barcode,
    PSSFAAssets.CustomDate01 = xhh_quickscan.Date1,
    PSSFAAssets.CustomDate02 = xhh_quickscan.Date2,
    PSSFAAssets.CustomDate03 = xhh_quickscan.Date3,
    PSSFAAssets.CustomDate00 = xhh_quickscan.Date4,
    PSSFAAssets.SerialNo = xhh_quickscan.Serial,
    PSSFAAssets.Custom10Char02 = xhh_quickscan.AStatus,
    PSSFAAssets.CustomDate04 = xhh_quickscan.Scandate
From
  (select * from xhh_quickscan 
  where crtdate  > '8/17/2013 7:55 AM'
  and crtdate  <= '8/18/2013 2:37 PM')

ON PSSFAAssets.AssetID = xhh_quickscan.AssetID

抛出错误:“关键字'ON'附近的语法不正确。”
尝试“ON”的子“WHERE”;没有运气。

3 个答案:

答案 0 :(得分:2)

为什么不简单?子查询对我来说似乎是多余的。

UPDATE p
SET ProjectID      = s.ProjectID,
    CpnyAssetNo    = s.Barcode,
    CustomDate01   = s.Date1,
    CustomDate02   = s.Date2,
    CustomDate03   = s.Date3,
    CustomDate00   = s.Date4,
    SerialNo       = s.Serial,
    Custom10Char02 = s.AStatus,
    CustomDate04   = s.Scandate
FROM dbo.PSSFAAssets AS p
INNER JOIN dbo.xhh_quickscan AS s
ON p.AssetID = s.AssetID
WHERE s.crtdate >  '20130817 07:55'
  AND s.crtdate <= '20130818 14:37';

答案 1 :(得分:1)

这是SQL Server上UPDATE JOIN的正确语法:

UPDATE A
SET A.ProjectID = B.ProjectID,
    A.CpnyAssetNo = B.Barcode,
    A.CustomDate01 = B.Date1,
    A.CustomDate02 = B.Date2,
    A.CustomDate03 = B.Date3,
    A.CustomDate00 = B.Date4,
    A.SerialNo = B.Serial,
    A.Custom10Char02 = B.AStatus,
    A.CustomDate04 = B.Scandate
FROM PSSFAAssets A
INNER JOIN (SELECT * 
            FROM xhh_quickscan 
            WHERE crtdate  > '8/17/2013 7:55 AM'
            AND crtdate  <= '8/18/2013 2:37 PM') B
ON A.AssetID = B.AssetID

答案 2 :(得分:0)

这将是最佳方式。

Update PSSFAAssets
Set PSSFAAssets.ProjectID = xhh_quickscan.ProjectID,
    PSSFAAssets.CpnyAssetNo = xhh_quickscan.Barcode,
    PSSFAAssets.CustomDate01 = xhh_quickscan.Date1,
    PSSFAAssets.CustomDate02 = xhh_quickscan.Date2,
    PSSFAAssets.CustomDate03 = xhh_quickscan.Date3,
    PSSFAAssets.CustomDate00 = xhh_quickscan.Date4,
    PSSFAAssets.SerialNo = xhh_quickscan.Serial,
    PSSFAAssets.Custom10Char02 = xhh_quickscan.AStatus,
    PSSFAAssets.CustomDate04 = xhh_quickscan.Scandate
From xhh_quickscan 
where xhh_quickscan.crtdate  > '8/17/2013 7:55 AM'
  and xhh_quickscan.crtdate  <= '8/18/2013 2:37 PM'
  and PSSFAAssets.AssetID = xhh_quickscan.AssetID