我正在尝试在AS400中运行包含if-else语句的SQL,但是它不起作用。我正在使用i Series Navigator创建一个View,以便运行它。
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 . .
我没有写就尝试过null但相反
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表达式代替:
CASE
SELECT CASE WHEN FIELD1 IS NOT NULL THEN 'AAA' ELSE 'BBB' END FROM LIB.TABLE1;
IF 是控制流构造:
IF
IF condition THEN SELECT ... ELSE SELECT ... END IF