1

所以我一直在尝试将我的数据库从 Access-2010 迁移到 SQL-2012。

大多数表和查询都可以,但是我在这方面遇到了麻烦:

 IIf([mkt_Original].[IMPOEXPO]="1",IIf([prod]="0201",IIf(([USD]/[Tons])
   <[TCambio].[CortePrecio0201],1,0),IIf([prod]="0202",IIf(([USD]/[Tons])
   <[TCambio].[CortePrecio0202],1,0),1)),1) AS GPrecio,

所以我尝试了 CASE;

 CASE WHEN [mkt_Original].[IMPOEXPO]="1", 
THEN
 CASE WHEN [rod]="0201" 
    THEN
      CASE WHEN 
      [USD]/[Tons])<[TCambio].[CortePrecio0201] 
      THEN 1 
    ELSE 0 
ELSE 
    CASE WHEN
    [prod]="0202"
    THEN
        CASE WHEN  
        [USD]/[Tons])<[TCambio].[CortePrecio0202]
        THEN 1
    ELSE 0
    ELSE 1
    END
AS GPrecio,

当我尝试运行它时,我不断收到“CASE 附近的错误语法”。有什么我可能会丢失的吗?提前致谢!

4

3 回答 3

2

SQL Server 2012 现在支持IIF,那么为什么还要翻译成更冗长的CASE表达式呢?您真正需要做的就是将双引号 ( ") 更改为单引号 ( ') - 或者如果IMPOEXPO是数字列,则将其删除。

IIf([mkt_Original].[IMPOEXPO]='1',IIf([prod]='0201',IIf(([USD]/[Tons])
  <[TCambio].[CortePrecio0201],1,0),IIf([prod]='0202', 
  IIf(([USD]/[Tons])<[TCambio].[CortePrecio0202],1,0),1)),1) AS GPrecio,
于 2013-12-10T20:52:31.263 回答
0

Use ' (apostrophe) instead of " (quotation mark) and don't forget to add an "END" for each case statement:

CASE WHEN condition THEN value1 ELSE value2 END

or

CASE variable
    WHEN value1 THEN value2
    WHEN value3 THEN value4
    ELSE value 3
END
于 2013-12-10T20:48:07.893 回答
0

CASE Statements are like this: CASE WHEN THEN ELSE END

So if you nest them, you have to END each nested CASE.

You can also format and simplify your code just a bit...

CASE
   WHEN [mkt_Original].[IMPOEXPO]="1", <--Remove the comma 
      THEN CASE
           WHEN [rod]="0201" AND [USD]/[Tons])<[TCambio].[CortePrecio0201] 
              THEN 1 
           WHEN [prod]="0202" AND [USD]/[Tons])<[TCambio].[CortePrecio0202] 
              THEN 1
           ELSE 0
           END
   ELSE 1
END
AS GPrecio,
于 2013-12-10T20:50:00.630 回答