Oracle Case语句分配值时出错

时间:2014-05-15 00:42:47

标签: sql oracle case

我希望在值为T时为TBL_C.PAY_CODE分配值“F”,但我似乎无法在此SQL语句中找到错误。我不想将PL / SQL用于这个普通的SQL语句。

ERROR

ORA-00905: missing keyword
00905. 00000 -  "missing keyword"

SQL CODE

SELECT
    TBL_A.ACT_ID,
    TBL_A.ACT_CODE,
    TBL_A.ACT_DESC,
    TBL_A.ACT_DIS_DESC,
    TBL_B.ACTMAP_ID,
    TBL_B.CLI_ID 
FROM CACT TBL_A, CMACTM TBL_B, CMACTDM TBL_C 
WHERE TBL_A.ACT_ID=TBL_B.ACT_ID AND B.ACTMAP_ID=TBL_C.ACTMAP_ID 
AND TBL_C.CODE = 'Version64'
AND TBL_C.ID='100001000000001'
AND TBL_C.PAY_CODE= CASE
  WHEN TBL_C.PAY_CODE='T' AND TBL_C.CODE ='Version64'
  THEN TBL_C.PAY_CODE='F' <-- ERROR 
  else TBL_C.PAY_CODE='T'
  END;

更新

此查询用于检索具有以下表值的一行。当用户提交具有以下值但按Pay_Code等于T的按钮时,应检索下面的行。我无法更改pay_code的值,因为还会遇到其他问题。

表TBL_C

ACTMAP_ID          ID                PAY_CODE  CODE
100001000000100    100001000000001   F         Version64

使用原始查询:

SELECT TBL_A.ACT_ID, TBL_A.ACT_CODE, TBL_A.ACT_DESC, TBL_A.ACT_DIS_DESC, TBL_B.ACTMAP_ID, TBL_B.CLI_ID 
    FROM CACT TBL_A, CMACTM TBL_B, CMACTDM TBL_C 
    WHERE TBL_A.ACT_ID=TBL_B.ACT_ID AND B.ACTMAP_ID=TBL_C.ACTMAP_ID 
    AND TBL_C.CODE =? (Value is Version64)
    AND TBL_C.ID=? (Value is 100001000000001)
    AND TBL_C.PAY_CODE=? (Either T or F but in the value is set to F)

2 个答案:

答案 0 :(得分:2)

SELECT TBL_A.ACT_ID, TBL_A.ACT_CODE, TBL_A.ACT_DESC, TBL_A.ACT_DIS_DESC, TBL_B.ACTMAP_ID, TBL_B.CLI_ID 
FROM CACT TBL_A, CMACTM TBL_B, CMACTDM TBL_C 
WHERE TBL_A.ACT_ID=TBL_B.ACT_ID AND B.ACTMAP_ID=TBL_C.ACTMAP_ID 
AND TBL_C.CODE = 'Version64'
AND TBL_C.ID='100001000000001'
AND TBL_C.PAY_CODE= CASE
  WHEN TBL_C.PAY_CODE='T' AND TBL_C.CODE ='Version64'
  THEN 'F'    <-------------
  else 'T'    <-------------
  END;

注意:您比较TBL_C.CODE = 'Version64'两次的事实表明您的逻辑可能存在缺陷......

答案 1 :(得分:0)

好问题,迪马斯!我不清楚您所希望的逻辑,但似乎您希望返回BL_C.PAY_CODE='T'TBL_C.CODE ='Version64'BL_C.PAY_CODE='F'的记录。如果是这种情况,我会写下来:

SELECT
    TBL_A.ACT_ID,
    TBL_A.ACT_CODE,
    TBL_A.ACT_DESC,
    TBL_A.ACT_DIS_DESC,
    TBL_B.ACTMAP_ID,
    TBL_B.CLI_ID 
FROM CACT TBL_A, CMACTM TBL_B, CMACTDM TBL_C 
WHERE TBL_A.ACT_ID=TBL_B.ACT_ID AND B.ACTMAP_ID=TBL_C.ACTMAP_ID 
AND TBL_C.ID='100001000000001'
AND ((TBL_C.PAY_CODE='T' AND TBL_C.CODE ='Version64')
    OR TBL_C.PAY_CODE='F');

如果我误读了你想要的结果,请澄清并且我会更新答案。