网站首页 > 文章中心 > 其它

oracle如何循环查询

作者:小编 更新时间:2023-08-16 13:51:29 浏览量:160人看过

oracle如何实现遍历查询?

declare

cursor t_name is select teachername from teacher---------申明游标t_name为从teacher表中查询老师名字

begin

open t_name;------打开游标t_name

loop-------开始循环(遍历)

fetch t_name into teacher_name-------将老师名字值赋予变量teacher_name

if t_name%found-------------开始遍历有值时插入以下数据

then

select name,count(*) into new_table

from table_teacher_student

where name=teacher_name group by name-----将一个老师名字依据条件插入新表数据

else

dmbs_output.put_line('完成所有工作');---------遍历结束时输出完成工作

exit;

end if;

end loop;

仓促写下以上内容,可能部分语法报错,思路就是这样,很基本的一个游标使用.

Oracle循环的几种写法(GOTO 、FOR 、 WHILE 、LOOP)

第一段:GOTO循环用法

DECLARE

? x number;

BEGIN

? repeat_loop --循环点

? x := x - 1;

? dbms_output.put_line(x);

? IF x 0 THEN

? ? ? GOTO repeat_loop;? --当x的值0时,就goto到repeat_loop

? END IF;

END;

/*以上语句翻译如下:

declare 定义变量;

begin...end语句块

? x 变量赋值

? repeat_loop 设置循环点

? 循环内容

? ? ? x 变量递减

? ? ? 按行打印 x

? IF...END IF语句块

? ? ? IF...(条件) THEN :满足IF条件? 则

? ? ? GOTO语句 前往循环点

*/

第二段:FOR循环用法

? ? ? dbms_output.put_line(i);

? END LOOP;

--最简单的循环?

/*

declare 定义变量

? for...loop...end loop; 语句

第三段:WHILE循环用法

? WHILE x 1 LOOP

? ? ? x := x - 1;

? ? ? dbms_output.put_line('循环内'||x);

? dbms_output.put_line('循环外'||x);

begin...end 语句块

? while...loop...end loop; 语句

第四段:LOOP循环用法

? x number;

? x :=0;

? LOOP

? ? ? ? x := x ◆ 1;

? ? ? ? dbms_output.put_line('内'||x);

? END LOOP;

? dbms_output.put_line('外'||x);

begin...end

? x 变量赋值

? loop...end loop语句

? ? ? exit when ...(条件) ;

ORACLE如何写循环查询?

oracle 循环查询问题

FROM USER_TABLES T

WHERE T.TABLE_NAME LIKE 'amd_%'

ORDER BY T.TABLE_NAME;

执行之后,把显示出来的句子copy出来,然后把双引号替换掉就好了.

oracle怎么循环查询下一条记录

以下为样例:

......

type tab_partition_name is table of all_ind_partitions.partition_name%type;

type tab_index_name is table of all_indexes.index_name%type;

type tab_last_analyzed is table of all_indexes.last_analyzed%type;

vt_partition_nametab_partition_name;

vt_index_nametab_index_name;

vt_last_analyzed tab_last_analyzed;

o_err := 'Successfully!';

v_owner := upper(rtrim(i_owner));

v_tablename := upper(rtrim(i_tablename));

-- select indexes of the table to rebuild

-- Command: alter index index name rebuild [partition partition name] online;

select a.index_name,b.partition_name,a.last_analyzed

bulk collect into vt_index_name,vt_partition_name,vt_last_analyzed

from all_indexes a,all_ind_partitions b

where a.table_owner=v_owner

and a.table_name=v_tablename

and a.owner=b.index_owner(◆)

and a.index_name=b.index_name(◆);

if vt_index_name.FIRST IS NULL OR vt_index_name.FIRST=0 THEN

-- 'No index to rebuild!'

o_err := 'The table of ['||v_owner||'.'||v_tablename||'] has not any index to rebuild!';

RETURN;

END IF;

for i_index in vt_index_name.FIRST..vt_index_name.LAST loop

if vt_partition_name(i_index) is null or length(trim(vt_partition_name(i_index)))=0 then

-- can not rebuild online in store procedure

if vt_last_analyzed is null then

v_execsql := 'alter index '||v_owner||'.'||vt_index_name(i_index)||' rebuild online';

v_execsql := 'alter index '||v_owner||'.'||vt_index_name(i_index)||' rebuild compute statistics online';

end if;

o_err := 'Alter index ['||v_owner||'.'||vt_index_name(i_index)||'] rebuild failed!';

v_execsql := 'alter index '||v_owner||'.'||vt_index_name(i_index)||' rebuild partition '||vt_partition_name(i_index)||' online';

v_execsql := 'alter index '||v_owner||'.'||vt_index_name(i_index)||' rebuild partition '||vt_partition_name(i_index)||' compute statistics online';

o_err := 'Alter index ['||v_owner||'.'||vt_index_name(i_index)||':'||vt_partition_name(i_index)||'] rebuild failed!';

execute immediate v_execsql;

exception

when others then

o_err := o_err||chr(10)||sqlerrm;

return;

end;

oracle 多表循环查询

个人认为没有,既然你是分表的,那么就应该没有,不管你怎么查询,最后都要落到union上.

个人认为你可以考虑分区表,在表中加两个字段一个月字段,一个年字段,以月字段分区,日字段子分区(根据数据量大小自行判断.)这样在一张表内,你想查询一年的数据就简单多了.

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章