我有2张桌子。一种具有订单元素 (OE),一种具有项目信息 (PO)。1个项目有很多订单元素。表格的设置方式,项目日期在 PO 中,货币在 OE 中。我需要更新 OE 表中的欧元汇率。我正在尝试做这样的事情
UPDATE [OETest]
SET [Euro Exchange Rate] = {
CASE
WHEN (DATEPART(month, PO.[Project Date Time]) = January)
THEN 8.143296
WHEN (DATEPART(month, PO.[Project Date Time]) = February)
THEN 8.340111
}
FROM [POTest] PO, [OETest] OE
WHERE OE.[Currency] = 'YUAN'
Run Code Online (Sandbox Code Playgroud)
但是我迷路了(这是我尝试过的许多查询之一)。任何人都可以帮助我构建必要的查询并告诉我它为什么起作用吗?
这个特定的查询告诉我关键字 CASE 附近有不正确的语法
为了更清楚地说明我要完成的任务:我在 OE 表中有一个欧元汇率列。我有从网站上获得的平均每月汇率(不在表格中)。我想根据项目的月份和货币来设置这个汇率列。我将在单独的查询中处理每种货币,因此人民币是我为此查询担心的唯一货币。月份在 PO 表中。我需要在 case 语句中使用 PO 表中的月份。
UPDATE OE -- the alias here rather than the base table name
SET [Euro Exchange Rate] = 8.143296
FROM [POTest] PO
JOIN [OETest] OE ON OE.project_id = PO.project_id -- you need a link
WHERE OE.[Currency] = 'YUAN'
-- the following date range represents January this year
AND PO.[Project Date Time] >= '20120101'
AND PO.[Project Date Time] < '20120201'
Run Code Online (Sandbox Code Playgroud)
如果根据不同的日期需要不同的值,则只需要一个 case 语句
UPDATE OE -- the alias here rather than the base table name
SET [Euro Exchange Rate] =
CASE Month(PO.[Project Date Time])
when 1 then 8.143296
when 2 then 7.143296
when 3 then 7.743296
END
FROM [POTest] PO
JOIN [OETest] OE ON OE.project_id = PO.project_id -- you need a link
WHERE OE.[Currency] = 'YUAN'
-- the following date range represents 3 months this year
AND PO.[Project Date Time] >= '20120101'
AND PO.[Project Date Time] < '20120401'
Run Code Online (Sandbox Code Playgroud)