Oracle sql语句中不支持boolean类型(decode&case)版本信息:
SQL> select * from v$version;BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production假设我们现在想知道1>0是否为真:直接查,不行!
SQL> select 1>0 from dual;
select 1>0 from dual
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected用decode转,还是不行!
SQL> select decode(1>0,true,"true","false") from dual;
select decode(1>0,true,"true","false") from dual
*
ERROR at line 1:
ORA-00907: missing right parenthesis用case转,依旧不行!
SQL> select case 1>0 when true then "true" else "false" end from dual;
select case 1>0 when true then "true" else "false" end from dual
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected原因在于sql不支持boolean类型(手头没有其他库,不知道mysql和sqlserver是否支持):
SQL> create or replace function is_true return boolean
is
begin
return true;
end;
/Function created.SQL> select is_true from dual;
select is_true from dual
*
ERROR at line 1:
ORA-06552: PL/SQL: Statement ignored
ORA-06553: PLS-382: expression is of wrong type直接放sql语句中行不通,试着放到fuction里:
decode还是出错:
SQL> CREATE OR REPLACE FUNCTION is1gt0
RETURN VARCHAR2
IS
BEGIN
RETURN DECODE (1 > 0, TRUE, "true", "false");
END;
/Warning: Function created with compilation errors.SQL> show err;
Errors for FUNCTION IS1GT0:LINE/COL ERROR
-------- -----------------------------------------------------------------
5/3 PL/SQL: Statement ignored
5/10 PLS-00306: wrong number or types of arguments in call to "DECODE"
SQL>case完美通过:
SQL> CREATE OR REPLACE FUNCTION is1gt0
RETURN VARCHAR2
IS
BEGIN
RETURN CASE 1 > 0
WHEN TRUE
THEN "true"
ELSE "false"
END;
END;
/Function created.SQL> show err;
No errors.
SQL> select is1gt0 from dual;IS1GT0
--------------------------------------------------------------------------------
trueSQL>
小结:
1. Oracle sql语句中不支持boolean类型;
2. decode是oracle独有的;而case是标准sql,mysql和sqlserver也可以使用,而且case还能把boolean转换输出。Oracle EBS如何enable 请求界面 View XML按钮Oracle EBS R12如何更改SYSADMIN的密码相关资讯 SQL语句
- 如何定位SQL语句在共享池里用到了 (03月17日)
- Java 注解入门 自动生成SQL语句 (07/28/2015 16:08:34)
- Oracle 通过sql profile为sql语句 (05/03/2015 19:43:07)
| - MySQL 存储过程动态执行sql语句 (10/13/2015 19:10:08)
- 画图解释 SQL join 语句 (07/17/2015 15:16:27)
- MySQL数据库sql语句调优 (03/21/2015 17:42:45)
|
本文评论 查看全部评论 (2)
评论声明- 尊重网上道德,遵守中华人民共和国的各项有关法律法规
- 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
- 本站管理人员有权保留或删除其管辖留言中的任意内容
- 本站有权在网站内转载或引用您的评论
- 参与本评论即表明您已经阅读并接受上述条款
| |