问题:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
2015-09-08 10:30
1456 查看
怎么写了一个语句带出这样的结果。
语句:
if exists (select * from sysdatabases where name='omni') then
结果:
ERROR 位于第 4 行:
ORA-06550: 第 4 行, 第 6 列:
PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用
ORA-06550: 第 4 行, 第 3 列:
PL/SQL: Statement ignored
FT死了,难道不能用这个函数吗?应该可以吧。另外,感觉用oracle出了问题的时候,不好查资料(和VC++相比),各位遇到问题都到哪里查资料呢?谢谢了!
——————————————————————————————————————————————————————————————————————————————————————
这个问题好像哪里见过。
oracle里exists是不能这样用的。必须放在sql语句中使用。比如
select * from dual where exists (select * from sysdatabases where name='omni')
以上只是例子,在pl/sql里的select语句还必须有into子句。
或者你可以用游标实现啊,很多方法能满足你的需求,不要拘泥与exists一种吧。
——————————————————————————————————————————————————————————————————————————————————————
exists的用法不是这样的。这个用法是SQL Server中的用法。
你在PL/SQL中可以这样:
select count(*) into ll_count from sysdatabases where name='omni';
if ll_count > 0 then
......
——————————————————————————————————————————————————————————————————————————————————————
第一种是最常用的,判断count(*)的值是否为零,如下
declare
v_cnt number;
begin
select count(*) into v_cnt from T_VIP where col=1;
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
首先这种写法让人感觉很奇怪,明明只需要知道表里有没有记录,却去统计了全表的记录数。
这种方式对于小表而言可以接受,一旦表记录很多的时候,性能问题就非常严重
因此有人就作了些修改,改成 select count(*) into v_cnt from T_VIP where col=1 and rownum=1
看起来似乎解决了性能问题,但是分析执行计划可以知道,实际上是一样的,不推荐使用。
第二种是所谓进攻式编程,不作预先判断,而是直接默认通过判断,然后使用 exception 来捕获异常
比如我这里不判断表中是否有满足条件的记录,默认它有,如果没有就在异常中进行处理
declare
v_1 number;
begin
select vip_level into v_1 from T_VIP where 1=0;
exception
when no_data_found then
dbms_output.put_line('无记录');
end;
这种方式从性能上讲比第一种要好得多
不过首先它没办法适应所有的情况,如第一段代码它就没办法改造
其次这种代码看起来让人觉得好像是发生了异常,而不是正常运行,从而造成混乱,不推荐使用。
第三种是利用 Oracle 原有的 Exists 语法,如下
declare
v_cnt number;
begin
select count(*)
into v_cnt
from dual
where exists (select * from t_vip where col=1);
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
通过在语句的外面套上一层dual,来使用oracle原有的exists语法
虽然和第一种看起来类似,但分析执行计划可以知道,性能比以上两种都要好得多,与MSSQL的 if exists 最接近,推荐使用。
可以把判断封装成一个函数以方便使用,代码如下
CREATE OR REPLACE FUNCTION EXISTS2 (IN_SQL IN VARCHAR2)
RETURN NUMBER
IS
/**********************************************************
* 使用示例
* begin
* if EXISTS2('select * from dual where 1=1')=1 then
* dbms_output.put_line('有记录');
* else
* dbms_output.put_line('无记录');
* end if;
* end;
*****************************************************************/
V_SQL VARCHAR2(4000);
V_CNT NUMBER(1);
BEGIN
V_SQL := 'SELECT COUNT(*) FROM DUAL WHERE EXISTS (' || IN_SQL || ')';
EXECUTE IMMEDIATE V_SQL INTO V_CNT;
RETURN(V_CNT);
END;
-
对于常用的insert判断还有更简单的写法,比如以下代码
if not exists(select * from table1 where id=1)
insert into table1 values(1,'a');
可以改写成
insert
when (not exists(select * from table1 where id=1)) then
into table1
select 1 as id, 'a' as data from dual;
-
再比如以下的代码
if not exists(select * from table1 where id=2)
insert into table1 values(2,'b')
else
update table1 set data='b' where id=2;
可以改写成
merge into table1 his
using
(
select 2 as id, 'b' as data from dual
) src
on (his.id=src.id)
when matched then
update set his.data=src.data where id=src.id
when not matched then
insert values(src.id,src.data);
-
这里附带说下,有人喜欢把count(*)写成count(列名),不推荐后一种,因为列名是需要额外的操作,去查询系统表来定位列信息
另外count(1)和count(*)没有差别,推荐使用count(*)直观明了
语句:
if exists (select * from sysdatabases where name='omni') then
结果:
ERROR 位于第 4 行:
ORA-06550: 第 4 行, 第 6 列:
PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用
ORA-06550: 第 4 行, 第 3 列:
PL/SQL: Statement ignored
FT死了,难道不能用这个函数吗?应该可以吧。另外,感觉用oracle出了问题的时候,不好查资料(和VC++相比),各位遇到问题都到哪里查资料呢?谢谢了!
——————————————————————————————————————————————————————————————————————————————————————
这个问题好像哪里见过。
oracle里exists是不能这样用的。必须放在sql语句中使用。比如
select * from dual where exists (select * from sysdatabases where name='omni')
以上只是例子,在pl/sql里的select语句还必须有into子句。
或者你可以用游标实现啊,很多方法能满足你的需求,不要拘泥与exists一种吧。
——————————————————————————————————————————————————————————————————————————————————————
exists的用法不是这样的。这个用法是SQL Server中的用法。
你在PL/SQL中可以这样:
select count(*) into ll_count from sysdatabases where name='omni';
if ll_count > 0 then
......
——————————————————————————————————————————————————————————————————————————————————————
Oracle中没有 if exists(...)
对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种第一种是最常用的,判断count(*)的值是否为零,如下
declare
v_cnt number;
begin
select count(*) into v_cnt from T_VIP where col=1;
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
首先这种写法让人感觉很奇怪,明明只需要知道表里有没有记录,却去统计了全表的记录数。
这种方式对于小表而言可以接受,一旦表记录很多的时候,性能问题就非常严重
因此有人就作了些修改,改成 select count(*) into v_cnt from T_VIP where col=1 and rownum=1
看起来似乎解决了性能问题,但是分析执行计划可以知道,实际上是一样的,不推荐使用。
第二种是所谓进攻式编程,不作预先判断,而是直接默认通过判断,然后使用 exception 来捕获异常
比如我这里不判断表中是否有满足条件的记录,默认它有,如果没有就在异常中进行处理
declare
v_1 number;
begin
select vip_level into v_1 from T_VIP where 1=0;
exception
when no_data_found then
dbms_output.put_line('无记录');
end;
这种方式从性能上讲比第一种要好得多
不过首先它没办法适应所有的情况,如第一段代码它就没办法改造
其次这种代码看起来让人觉得好像是发生了异常,而不是正常运行,从而造成混乱,不推荐使用。
第三种是利用 Oracle 原有的 Exists 语法,如下
declare
v_cnt number;
begin
select count(*)
into v_cnt
from dual
where exists (select * from t_vip where col=1);
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
通过在语句的外面套上一层dual,来使用oracle原有的exists语法
虽然和第一种看起来类似,但分析执行计划可以知道,性能比以上两种都要好得多,与MSSQL的 if exists 最接近,推荐使用。
可以把判断封装成一个函数以方便使用,代码如下
CREATE OR REPLACE FUNCTION EXISTS2 (IN_SQL IN VARCHAR2)
RETURN NUMBER
IS
/**********************************************************
* 使用示例
* begin
* if EXISTS2('select * from dual where 1=1')=1 then
* dbms_output.put_line('有记录');
* else
* dbms_output.put_line('无记录');
* end if;
* end;
*****************************************************************/
V_SQL VARCHAR2(4000);
V_CNT NUMBER(1);
BEGIN
V_SQL := 'SELECT COUNT(*) FROM DUAL WHERE EXISTS (' || IN_SQL || ')';
EXECUTE IMMEDIATE V_SQL INTO V_CNT;
RETURN(V_CNT);
END;
-
对于常用的insert判断还有更简单的写法,比如以下代码
if not exists(select * from table1 where id=1)
insert into table1 values(1,'a');
可以改写成
insert
when (not exists(select * from table1 where id=1)) then
into table1
select 1 as id, 'a' as data from dual;
-
再比如以下的代码
if not exists(select * from table1 where id=2)
insert into table1 values(2,'b')
else
update table1 set data='b' where id=2;
可以改写成
merge into table1 his
using
(
select 2 as id, 'b' as data from dual
) src
on (his.id=src.id)
when matched then
update set his.data=src.data where id=src.id
when not matched then
insert values(src.id,src.data);
-
这里附带说下,有人喜欢把count(*)写成count(列名),不推荐后一种,因为列名是需要额外的操作,去查询系统表来定位列信息
另外count(1)和count(*)没有差别,推荐使用count(*)直观明了
相关文章推荐
- Nginx+Tomcat7+Memcached集群与负载均衡(超详细)
- 关于in 和 exist 的区别-------------近期优化sql 语句的时候再次碰到
- 根据表中的行创建一个分隔列表
- oracle存储过程写法
- 192.168.148.215\sql2000连不上
- mysql导出查询结果到文档
- oracle查询数据库名、实例名等
- JDBC常用数据库驱动连接代码总结
- Oracle中常见的33个等待事件小结
- oracle function
- 数据库中的一个表始终修复不成功
- mybatis打印不出SQL语句的问题
- C# Redis 系列二
- MSSQL根据日志恢复数据库
- 《高性能MySQL》第一章
- sql中top使用方法
- distribute mysql
- Java中使用Jedis操作Redis
- sql server 2008 评估期已过期解决办法
- C# Redis 系列一