oracle 游标的使用
create or replace procedure P_TRAN_SIMULATION1
(
v_ComputeDate1 in date,
v_ComputeDate2 in date
)
/* Oracle:Authid Current_User的使用
我们知道,用户拥有的role权限在存储过程是不可用的。遇到这种情况,我们一般需要显式授权,
如grant create table to usera;但这种方法太麻烦,有时候可能需要进行非常多的授权才能执行存储过程,
实际上,oracle给我们提供了在存储过程中使用role权限的方法:修改存储过程,加入Authid Current_User时存储过程可以使用role权限。下面来举个例子: */
authid current_user
is vn_ctn number(2);
begin
select count(*) into vn_ctn from user_all_tables a where a.table_name like upper('tbl');
if vn_ctn > 0 then
execute immediate 'drop table tbl';
end if;
execute immediate 'create table tbl(id1 number,id2 number)';
COMMIT;
declare cashflowPOSITIVE NUMBER:=0;
cashflowNEGATIVE NUMBER:=0;
cursor mycur is
select *
from TRAN_CASH_FLOW
where as_of_date = v_ComputeDate1
and payment_date >=v_ComputeDate1
and payment_date < v_ComputeDate2;
cashFlowRow TRAN_CASH_FLOW%rowtype;
begin
open mycur; --打开游标
loop
fetch mycur into cashFlowRow; --把游标所指的纪录放到变量中
exit when (mycur%notfound); --当游标没有指向行时退出循环
if cashFlowRow.cashflow_value>0 then
cashflowPOSITIVE:=cashflowPOSITIVE+cashFlowRow.cashflow_value;
else
cashflowNEGATIVE:=cashflowNEGATIVE+cashFlowRow.cashflow_value;
end if;
end loop;
dbms_output.put_line(cashflowPOSITIVE);
close mycur; --关闭游标
/* insert into tbl values(cashflowPOSITIVE,cashflowNEGATIVE);*/
end;
end P_TRAN_SIMULATION1;