declare
v_name varchar2(10) := null;
begin
begin
select 用户姓名 into v_name from 表A where 用户id ='0000011' and rownum = 1 ;
exception when no_data_found then
begin
select 用户姓名 into v_name from 表B where 用户id='0000011'and rownum = 1;
exception when no_data_found then null;
end;
end;
end;
v_name varchar2(10) := null;
begin
begin
select 用户姓名 into v_name from 表A where 用户id ='0000011' and rownum = 1 ;
exception when no_data_found then
begin
select 用户姓名 into v_name from 表B where 用户id='0000011'and rownum = 1;
exception when no_data_found then null;
end;
end;
end;
解决方案 »
- 属性相同的列转为多行
- 如何优化表连接SQL,我有两个这样的SQL,运行特别费时间,求优化方法.
- dbms_random包,在普通的plsql中可以使用,但是在我的package中引用就不行
- 求触发器
- 新手提问:right 无效的标识符问题
- 如何让32位oracle在32位2003server上占用50g内存。
- Oracle速度突然变慢,请大家指教
- 求助:请问有关oracle internet directory的问题!!!
- 紧急求救!那位大虾知道为什么Developer开发的程序点击数据列表的下拉条向下按钮不放会死机?
- 删除表释放表空间
- 如何用oracle进行冷备份
- 有谁用oracle AQ高级消息队列做过大型应用程序?
if v_name is null then
select 用户姓名 into v_name from 表B where 用户id='0000011';
end if;
begin
select 用户姓名 into v_name from 表A where 用户id='0000011';
exception
when others then
begin
select 用户姓名 into v_name from 表B where 用户id='0000011';
exception
when others then
....
end;
end;
end;
oracle 7.3 中,不用eception 有没有更好的方法实现下面的功能。
select count(*) into i from 表A where 用户id='0000011';
if i>0 then
select 用户姓名 into v_name from 表A where 用户id='0000011';
else
select count(*) into j from 表B where 用户id='0000011';
if j>0 then
select 用户姓名 into v_name from 表B where 用户id='0000011'; end if;
end if
if v_name='null' then
select nvl(max(用户姓名),'null') into v_name from 表B where 用户id='0000011';
if v_name='null' then
...
end if;
end if;
....
----------------
0SQL> select nvl(time,0) from test where 1=2;未选定行SQL> 使用max(),如果没有检索出结果 返回null
不用的话,触发exception操作
……
end if;
if i>0 then
select 用户姓名 into v_name from 表A where 用户id='0000011';
else
select count(*) into j from 表B where 用户id='0000011';
if j>0 then
select 用户姓名 into v_name from 表B where 用户id='0000011'; end if;
end if
select count(*) into i from 表A where 用户id='0000011' and rownum=1;
if i>0 then
select 用户姓名 into v_name from 表A where 用户id='0000011';
else
select count(*) into j from 表B where 用户id='0000011' and rownum=1;
if j>0 then
select 用户姓名 into v_name from 表B where 用户id='0000011'; end if;
end if ;
原先select count(*) into i from 表A where 用户id='0000011';要扫描整个表A来count(*),这并不是程序本意。程序里只要判断表A是否存在用户id='0000011'的记录就行。
加上 rownum=1后可以起到相同的功能,而且缩短了查询时间,这方法不错!
各位觉得如何?
1.如果表中有错误:i=2
将报出错误:select 用户姓名 into v_name from 表A where 用户id='0000011';
2.如果 用户id没建索引.
count(*) 肯定全表扫描.性能恶化.select NVL(用户姓名,'NULL') into v_name from 表B where
exists (select 1 from 表a where 表a.用户id=表B.用户id and 表a.用户id='0000011' and rownum=1)
if v_name='NULL' then
select 用户姓名 into v_name from 表A where 用户id='0000011' and rownum=1;end if;
是什么意思?
“2.如果 用户id没建索引. count(*) 肯定全表扫描.性能恶化.”
你是说select count(*) into i from 表A where 用户id='0000011' and rownum=1;
也要做全表扫描吗?
select 用户姓名 into v_name from 表A where 用户id='0000011';
就存在二个姓名了,into v_name会报措.
2.count(*)如果你用户id没有索引会出现TABLE ACCESS FULL .你可以试一下,看一下执行计划.
select 用户姓名 into v_name from 表A where 用户id='0000011' and rownum=1;
后面的rownum=1;也不能阻止TABLE ACCESS FULL 吗?
1 20 张三
33 20 里斯
345 20 往无select * from emp where 年龄=20 and rownum=1;
到底返回哪一行?是不是一定为 张三 所在的行
如果删除 张三 所在的行呢?
union
select 用户姓名,'B' into v_name ,v_flag from B
不知道这样行不行?
这样从逻辑上来说,如果A、B表中都能检索出结果,v_flag 是放'A' 还是'B'呢?
select count(*) into i from dual where exists (select 1 from 表A where 用户id='0000011')
if i>0 then
select 用户姓名 into v_name from 表A where 用户id='0000011';
else
select count(*) into j from dual where exists (select 1 from 表B where 用户id='0000011')
if j>0 then
select 用户姓名 into v_name from 表B where 用户id='0000011'; end if;
end if 谢谢 各位的关注。