1.PL/SQL中的存储过程
根据起始时间和结束时间查出来数据,并插入到fxgfsl_reporot表里。由于查出来的数据可能不止一条,要使用游标,oracle代码如下
create or replace procedure INSERT_FXR(starttime in varchar2,endtime in varchar2)
is
JE number(8,2);
cursor ptcursor is
select s.merit_name,m.merit_type,m.merit_amount,count(s.merit_id) as c from sacrifice_info s left join merit_info m on m.seqid=s.merit_id where m.merit_class=1 and s.registration_date>to_date(starttime,'yyyy-mm-dd') and s.registration_date<to_date(endtime,'yyyy-mm-dd') group by s.merit_name,s.merit_id,m.merit_amount,m.merit_type;
begin
for ptrecord in ptcursor
loop
JE:=ptrecord.c*ptrecord.merit_amount;
insert into fxgfsl_reprot values(fxgfslreport_seq.nextval,ptrecord.merit_type,ptrecord.merit_name,ptrecord.merit_amount,ptrecord.c,JE,to_date(starttime,'yyyy-mm-dd'),to_date(endtime,'yyyy-mm-dd'),sysdate);
end loop;
end INSERT_FXR;
2.java调用存储过程
然后是在程序里调用。程序使用了struts2和spring,继承了JdbcDaoSupport。使用JdbcTemplate调用的方式如下
/**
* 执行存储过程
*
* @param startDate
* @param endDate
*/
public void proc(final String startDate, final String endDate) {
String sql = "{call insert_fxr(?,?)}";
this.getJdbcTemplate().execute(sql, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
cs.setString(1, startDate); // 输入参数
cs.setString(2, endDate); // 输入参数
return cs.execute();
}
});
}