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;
仓促写下以上内容,可能部分语法报错,思路就是这样,很基本的一个游标使用.
第一段: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 ...(条件) ;
FROM USER_TABLES T
WHERE T.TABLE_NAME LIKE 'amd_%'
ORDER BY T.TABLE_NAME;
执行之后,把显示出来的句子copy出来,然后把双引号替换掉就好了.
以下为样例:
......
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;
个人认为没有,既然你是分表的,那么就应该没有,不管你怎么查询,最后都要落到union上.
个人认为你可以考虑分区表,在表中加两个字段一个月字段,一个年字段,以月字段分区,日字段子分区(根据数据量大小自行判断.)这样在一张表内,你想查询一年的数据就简单多了.