[英]Add inline function with IF/ELSE clause in Oracle 12c
我有表A。如果我使用內聯函數進行查詢
with function f(n number) return varchar2 as
begin
return 'const string';
end;
select id, val, count, f(count) as value from A;
結果如下:
ID VAL COUNT VALUE
---------- -------------------- ---------- ---------------
1 car 4 const string
2 building 15 const string
但是如果我嘗試使功能更復雜
with function f(n number)
return varchar2 as
begin
IF n < 5 THEN
return 'small';
ELSIF n < 50 THEN
return 'normal';
ELSE
return 'big';
END IF;
end;
select id, val, count, f(count) as value from A;
出現錯誤信息:
with function f(n number)
*
ERROR at line 1:
ORA-00905: missing keyword
這是什么問題 我對命令使用正確的語法嗎?
你if
語句缺少then
后elsif
子句條件,因此缺少關鍵字錯誤指向功能f
。 此外,解決此錯誤后,您可能會得到ORA-00933: SQL command not properly ended
指向最后一個分號。 有趣的是,“;” 當WITH子句中包含PL / SQL聲明時,似乎不能作為SQL語句的終止符。 如果我們嘗試單獨使用它,SQL * Plus將等待輸入更多文本。 因此,您必須在新行上以/
結束。 即使在《 SQL參考手冊》中的示例中,也使用了;
的組合;
和/
。 這是我在PL / SQL Developer 11中測試過的示例:
WITH
FUNCTION f(n number) return varchar2 IS
begin
if n<5 then
return 'small';
elsif (n>5 AND n<50) then
return 'medium';
else
return 'big';
end if;
end;
select f(25) from dual
/
輸出:
F(25)
medium
編輯:另外,在函數定義中將AS
更改為IS
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.