oracle存储过程结合我公司代码1


 1.           Framework.QueryInfo info1 = new Framework.QueryInfo();
            //string Sql = Holworth.Utility.Utility.EntityRowMapper.getSql("//data//ValueAtRiskCalculationService//NoneQuery2");
            //info.CustomSQL = Sql;
            info1.NamedQuery = "P_TRAN_SIMULATION1";
            info1.Parameters.Add("v_ComputeDate1", DateTime.Parse("2015/3/9"));
            info1.Parameters.Add("v_ComputeDate2", DateTime.Parse("2015/6/9"));
            Dao.ExecuteNonQuery(info1);

2.

create or replace procedure P_TRAN_SIMULATION1
(
 v_ComputeDate1 in date,
 v_ComputeDate2 in date,
 r_cursor1 OUT sys_refcursor
)
/* 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 --多条件就用and 和or 来进行连接
      execute immediate 'drop table tbl';  
   end if;
   
   execute immediate 'create table tbl(id1 number,id2 number)';   
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;  --关闭游标
      execute immediate 'insert into tbl values('||cashflowPOSITIVE||','||cashflowNEGATIVE||')';
   
   EXECUTE IMMEDIATE 'BEGIN OPEN :r_cursor1 FOR SELECT * FROM tbl; END;' using r_cursor1;
   close r_cursor1;
   end;
--execute immediate的作用
/* EXECUTE IMMEDIATE将不会提交一个DML事务执行
如果通过EXECUTE IMMEDIATE处理DML命令,
那么在完成以前需要显式提交或者作为EXECUTE IMMEDIATE自己的一部分.
如果通过EXECUTE IMMEDIATE处理DDL命令,它提交所有以前改变的数据*/
end ;




调用存储过程的代码
 call p_tran_simulation1(to_date('2015/03/09','yyyy/mm/dd'),
                       to_date('2015/06/09','yyyy/mm/dd'));
























posted on 2015-04-28 09:46  听哥哥的话  阅读(137)  评论(0编辑  收藏  举报

导航