DB2/400 中的 if-else 语句



我正在尝试运行一个在AS400中包含if-else语句的SQL,但它不起作用。我正在使用i系列导航器创建一个视图以运行它。

SELECT IF FIELD1 IS NOT NULL THEN 'AAA' ELSE 'BBB' END IF
FROM LIB.TABLE1

我得到的错误是:

SQL State: 42601
Vendor Code: -199
Message: [SQL0199] Keyword IS not expected. Valid tokens: , FROM INTO. Cause . . 

我尝试没有写是空的,而是

SELECT IF FIELD1 ='' THEN 'AAA' ELSE 'BBB' END IF
    FROM LIB.TABLE1

然后我得到以下错误:

SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token = was not valid. Valid tokens: , FROM INTO. Cause . . . . . :   A syntax error was detected at token =.  Token = is not a

改用CASE表达式:

SELECT CASE WHEN FIELD1 IS NOT NULL THEN 'AAA' ELSE 'BBB' END 
FROM LIB.TABLE1;

IF是控制流构造:

IF condition THEN 
   SELECT ...
ELSE 
   SELECT ...
END IF
SELECT ABC, DEF,
CASE WHEN (substring(GHI,1,4)) IS 'SOTH' THEN 'J' ELSE 'N' END as SOTH
WHERE ABC like 'ABC%'
GROUP BY ABC,DEF,GHI

相关内容

  • 没有找到相关文章

最新更新