更正此更新过程的正确sql语法

时间:2016-01-13 09:06:59

标签: sql sql-server-2014-express

我有三张桌子:

LandingDetails,包含LandingId,ProductId和UnitPrice

字段

具有LandingId和LandingDate字段的LandingHeaders

包含ProductId和CostPrice字段的产品

我希望根据落在两个日期之间的登陆日期中的登陆日期,从产品的成本价格中更新LandingDetails UnitPrice。

在第一次尝试时,我提出了以下内容,这可能与目标的概念一致,但显然错过了表连接。

cat /data/market_voices_spam_classifier/v5/logs/server.log | sh mapper.sh | python2.7 reducer.py | pee 'tee spammers-`date -d "-1 days" +%Y-%m-%d`' "EMAIL=noreply mutt -s 'Test Mail' mail_id@gmail.com"

第二版如下,但我怀疑连接不正确。

    CREATE PROCEDURE dbo.UpdateAndPriceLandings
@StartDate DATE,
@EndDate  DATE
  AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

UPDATE LandingDetails
  SET UnitPrice = (SELECT p.CostPrice FROM Products p WHERE p.ProductId = LandingDetails.ProductId AND 
  dbo.LandingHeaders.LandingId = LandingId AND dbo.LandingHeaders.LandingDate1 BETWEEN @StartDate AND @EndDate)
GO

任何人都可以指出我怀疑的是我的愚蠢的语法错误吗?

1 个答案:

答案 0 :(得分:4)

您在第二个SP中的查询应如下所示:

UPDATE
    dbo.LandingDetails
SET 
    UnitPrice=p.CostPrice
FROM
    dbo.LandingDetails ld
    INNER JOIN dbo.LandingHeaders lh ON 
        lh.LandingId=ld.LandingId
    INNER JOIN dbo.Products P ON 
        P.ProductId=ld.ProductId
 WHERE 
    lh.LandingDate BETWEEN @StartDate AND @EndDate