oracle 預存中 的迴圈!
/**************************====
查詢資料表 a10中的 a10_c4,a10_c5
a10_c4 相同時將a10_c5 寫到乙個table表中的一格字段中
*******************************/
procedure sp_a10_q6
(stra10_1_sta varchar2,--起始學年度(10)
stra10_1_end varchar2,--截止學年度(10)
stra10_c2 varchar2,-- a教學(30)
rc1 out cursr0001,
strmessage out varchar2,
strstatus out number)as
/************************************
--定義變數
************************************/
v_stra10_1_sta varchar2(10);--學年度(10)
v_stra10_1_end varchar2(10);--學年度(10)
v_stra10_c2 varchar2(30);--a教學(30)
ccount integer;
cnum integer;
/************************************
--定義cursor變數cursor1
************************************/
a10_c1 varchar(100);
a10_c2 varchar(100);
a10_c4 varchar(100);
cursor cursor1 is
select distinct a10_c1,a10_c2,a10_c4
from a10
where a10_c1>=stra10_1_sta
and a10_c1<=stra10_1_end
and a10_c2 = stra10_c2
order by a10_c1,a10_c2,a10_c4;
r_cursor1 cursor1%rowtype;
/************************************
--定義cursor變數cursor2
************************************/
a10_c1_2 varchar(100);
a10_c2_2 varchar(100);
a10_c4_2 varchar(100);
a10_c5_2 varchar(100);
cursor cursor2 is
select distinct
a10_c1 as a10_c1_2,
a10_c2 as a10_c2_2,
a10_c4 as a10_c4_2,
a10_c5 as a10_c5_2
from a10
where a10_c1>=stra10_1_sta
and a10_c1<=stra10_1_end
and a10_c2 = stra10_c2
order by a10_c1,a10_c2,a10_c4,a10_c5;
r_cursor2 cursor2%rowtype;
a10_c5_sql varchar2(1000);--重要措施(1000)
a10_c5b varchar2(100);--重要措施(100)
begin
ccount := 0;--相同目標下重要措施的資料筆數
cnum :=0;--變數
delete from a10b;
/************************************
--宣告變數
************************************/
v_stra10_1_sta := trim(stra10_1_sta);
v_stra10_1_end := trim(stra10_1_end);
v_stra10_c2 := trim(stra10_c2);
a10_c1 := '';
a10_c2 := '';
a10_c4 := '';
a10_c1_2 := '';
a10_c2_2 := '';
a10_c4_2 := '';
a10_c5_2 := '';
a10_c5b := '';--中間變數
a10_c5_sql := '';
strmessage := '';
open cursor1;
loop
fetch cursor1 into r_cursor1;
exit when cursor1%notfound;
open cursor2;
select count(*) into ccount
from a10
where a10_c1>=stra10_1_sta
and a10_c1<=stra10_1_end
and a10_c2 = stra10_c2;
loop
fetch cursor2 into r_cursor2;
exit when cursor2%notfound;
if(cnum<=ccount) then
begin
if (r_cursor1.a10_c1=r_cursor2.a10_c1_2 and r_cursor1.a10_c2=r_cursor2.a10_c2_2 and r_cursor1.a10_c4=r_cursor2.a10_c4_2) then
begin
cnum := cnum+1;
a10_c5b := cnum||'.'||trim(r_cursor2.a10_c5_2);
a10_c5_sql :=trim(a10_c5_sql)||trim(a10_c5b)||'/n';
end;
end if;
end;
end if;
end loop;
close cursor2;
insert into a10b
(a10b_c1,a10b_c2,a10b_c3,a10b_c4,a10b_c5
)values
(trim(r_cursor1.a10_c1), trim(r_cursor1.a10_c2),'',
trim(r_cursor1.a10_c4),trim(a10_c5_sql)
);end loop;
--close cursor2;
close cursor1;
/************************************
--取出最後的結果結合cursor
a10b實體表。存data
************************************/
open rc1 for
select *
from a10b;
/************************************
--例外處理
************************************/
exception
when no_data_found then
strmessage := '資料處理失敗!!';
strstatus := 2;
when others then
strmessage := sqlerrm;
strstatus := 1;
Oracle中的迴圈
主要有以下五種迴圈 exit when loop while for 普通迴圈 for 游標迴圈 下面舉例一一說明 均為儲存過程 1 exit when迴圈 create or replace procedure proc test exit when is i number begin i 0 l...
oracle 中退出迴圈的用法
declare i integer j integer begin i 1 j 1 for i in 1 10 loop 主迴圈 for j in 1 10 loop 次迴圈 exit main loop when i 2 if j 3 then exit 退出當前迴圈 end if if i 5 ...
Oracle中的loop迴圈的例子
oracle中的loop迴圈的例子 第一 loop.exit when.end loop sql declare temp salary employee.salary type temp emp employee rowtype cursor mycursor is select from emp...