“THEN”或其附近的Postgresql语法错误

时间:2016-02-16 09:00:43

标签: sql postgresql syntax-error create-view

我正在尝试在Postgresql中创建一个视图,但是当我运行此代码时出现此错误:

  在语法错误

CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
    SELECT
        P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
        P.VALID AS VALID,
         [...]
        IF (VALID == FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID) AS DESCRIPTION,
         [...]
    FROM public.TA_POSIZIONI_DEVICE P
    JOIN ...

TA_POSIZIONI_DEVICE

  • VALID(布尔值非null)

3 个答案:

答案 0 :(得分:2)

您应该使用CASE

  

SQL CASE 表达式是一个通用的条件表达式,类似    if / else 其他编程语言中的语句

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
END

所以,

CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
    SELECT
        P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
        P.VALID AS VALID,
         [...]
           CASE WHEN VALID = false THEN 'Valid'
                ELSE P.REASON_FOR_INVALID
           END AS DESCRIPTION,

         [...]
    FROM public.TA_POSIZIONI_DEVICE P
    JOIN ...

答案 1 :(得分:1)

你可以使用案例

case when VALID = FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID end DESCRIPTION,

答案 2 :(得分:-1)

IF (VALID == FALSE) THEN 'Valid' ELSE P.REASON_FOR_INVALID END IF AS DESCRIPTION

试试这个!你条件中有一个家长问题