我对 PL/SQL 完全陌生。我编写了以下 PL/SQL 脚本。但它不执行并给出编译错误:
set serveroutput on SIZE 1000000;
IF EXISTS (select * from my_table)
begin
dbms_output.put_line('has rows');
end;
else
begin
dbms_output.put_line('no rows');
end;
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这有什么问题?
我怎样才能做到这一点?
Bal*_*app 14
匿名 PL/SQL 块不以IF
. 上面的代码应该在 abegin
和end;
at least之间。
EXISTS
是一个 SQL 函数,它不能像那样在 PL/SQL 中使用。
尝试这样的事情:
set serveroutput on
declare
c number;
begin
select count(*) into c from my_table where rownum = 1;
if c != 0 then
dbms_output.put_line('has rows');
else
dbms_output.put_line('no rows');
end if;
end;
/
Run Code Online (Sandbox Code Playgroud)
是的,EXISTS
也可以在查询中使用:
set serveroutput on
declare
c varchar2(10);
begin
select case when exists (select * from my_table) then 'has rows' else 'no rows' end into c from dual;
dbms_output.put_line(c);
end;
/
Run Code Online (Sandbox Code Playgroud)
请注意, theEXISTS
和rownum = 1
version 都将在找到的第一行处停止,这就是重点,因此我们不需要读取整个表/索引。
小智 6
如果语法错误。并且这里不允许存在。试试这个方法:
declare
cnt number;
begin
select count(*) into cnt from my_table;
if cnt != 0 then
dbms_output.put_line('has rows');
else
dbms_output.put_line('no rows');
end if;
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23731 次 |
最近记录: |