can not do this to use a sql statement.
use function:...
v_i integer;
v_key a.key%type;
cursor c is select key from a order by key desc;
...
begin
open c;
fetch c into v_key;
v_i:=v_key;
fetch c into v_key;
while c%found loop
if (v_i>v_key+1)
result:=v_i-1;
else
v_i:=v_key;
end if;
fetch c into v_key;
end loop;
return result;
...
use function:...
v_i integer;
v_key a.key%type;
cursor c is select key from a order by key desc;
...
begin
open c;
fetch c into v_key;
v_i:=v_key;
fetch c into v_key;
while c%found loop
if (v_i>v_key+1)
result:=v_i-1;
else
v_i:=v_key;
end if;
fetch c into v_key;
end loop;
return result;
...
解决方案 »
- 请高手老鸟们帮帮忙把我的查询语句优化一下,谢谢了~
- 求一个SQL语句
- 因为执行了错误的SQL语句,TEMP01.DBF文件一下子增大了很多G,把磁盘空间都占用了。怎么办呢?
- 问一个where in 里面用占位符的问题
- 帮忙...谁帮给我一下函数~~~急
- 请教:关于Asp操作oracle,如何在记录集中给一个字段,插入序列值
- 请问各位,能不能说说在开发和使用中,oracle具体好在哪里
- (9)菜鸟问题!!!容易拿分!!
- Database Link与Socket那种方式更好?
- 一个存储过程,出现错误,请问如何解决,急!
- 数据表连接速度很慢???
- 请问PL/SQL中union进来的后的结果如何进行排序???
(
select key from yourtable a where not exists ( select 1 from yourtable b where to_number(a.key) = to_number(b.key) - 1)
order by key desc
) c
where rownum < 2;
TABLE1:
KEY1:
1
2
...
9999
TABLE1中包含了全部1-9999的KEY
TABLE2:
KEY2:
3
8
100
TABLE2中是实际数据库里存放的KEY那么SQL为select max(key1)
from table1
where not exist(select key2 from TABLE2)这个方法怎么样?效率与上两种方法相比那个更快一些呢?
return varchar2
as
v_mul varchar2(10);
cursor t_sor is
select lead(id,1,1) over(order by id desc) next_id,lead(id,1,1) over(order by id desc)-id mul_id from table_name;
begin
for v_sor in t_sor loop
if v_sor.mul_id=0 then
return 0;
exit;
elsif v_sor.mul_id>1 then
v_mul:=v_sor.mul_id;
end if;
end loop;
return v_mul;
end;
/create or replace trigger table_tri
before insert on table_name
for each row
begin
:new.id:=get_id;
end;
/
create or replace function get_id
return varchar2
as
v_mul varchar2(10);
cursor t_sor is
select lead(id,1,1) over(order by id desc) next_id,lead(id,1,1) over(order by id desc)-id mul_id from table_name;
begin
for v_sor in t_sor loop
if v_sor.mul_id=0 then
return 0;
exit;
elsif v_sor.mul_id>1 then
v_mul:=v_sor.mul_id-1; --此处就是返回的最近值
end if;
end loop;
return v_mul;
end;
/
~
这个“1”是什么意思?
---------- ---------
a 1
a 3
a 5
a 6实际:70
12:26:29 SQL> select count(*) from col; COUNT(*)
---------
120实际:61
12:26:42 SQL> select max(id)from (
12:26:48 2 select tcn.*,t.id from tcn,(select rownum id from col) t
12:26:48 3 where t.id=tcn.bbb(+) and t.id<=(select max(bbb) from tcn)
12:26:48 4 ) tb where tb.aaa is null ; MAX(ID)
---------
4实际:60
12:26:48 SQL>
12:26:48 SQL> insert into tcn
12:28:16 2 select 'a',max(id)from (
12:28:33 3 select tcn.*,t.id from tcn,(select rownum id from col) t
12:28:35 4 where t.id=tcn.bbb(+) and t.id<=(select max(bbb) from tcn)
12:28:35 5 ) tb where tb.aaa is null ;已创建 1 行。实际:701
12:28:37 SQL> select * from tcn;AAA BBB
---------- ---------
a 1
a 3
a 5
a 6
a 4实际:70
12:28:42 SQL>
insert into test1 values('0001');
insert into test1 values('0009');
insert into test1 values('0123');
insert into test1 values('0321');
insert into test1 values('9023');
insert into test1 values('9998');
insert into test1 values('9999');
select max(newc1)
from (select c1,lpad(10000-rownum,4,'0') newc1
from (select c1
from test1 order by c1 desc))
where c1<>newc1;