oracle视图实现动态传条件

在Oracle数据库中,经常会有传递参数到视图中的需求,但是由于视图不能直接使用参数,导致数据库中很多复杂的逻辑需要使用存储过程或函数实现。那么如何在Oracle视图中传递参数呢?下面我们介绍一种做法,来实现视图中参数传递的技巧:

使用动态SQL语句实现视图参数传递

使用动态SQL,我们可以通过将参数作为变量传递给视图,然后在SQL语句中使用语句进行参数引用,具体实现如下:
–这里写了3个,类型都是varchar2,分别是begintimes 、endtimes 、related_trans_scopes
–vm_param 为名字。可以根据这个调用参数(个人感觉像实体类生成get、set方法,可以实体.下面的属性)

1、执行脚本 ,创建包体 :

create or replace package vm_param  is
	function set_begintime(begintimes varchar2) return varchar2;
    function get_begintime  return varchar2;
    function set_endtime(endtimes varchar2) return varchar2;
    function get_endtime  return varchar2;
    function set_related_trans_scope(related_trans_scopes varchar2) return varchar2;
    function get_related_trans_scope  return varchar2;
end vm_param  ;

2执行脚本,创建包体内容


--名字和上面的要一样
create or replace package body vm_param is
--定义字段类型长度
	begintime varchar2(100);
	endtime varchar2(100);
	related_trans_scope varchar2(500);
		function set_begintime(begintimes varchar2) return varchar2 is
			begin
			begintime:=begintimes;
			return begintime;
			end;
		function get_begintime return varchar2 is
			begin
			return begintime;
			end;
		function set_endtime(endtimes varchar2) return varchar2 is
			begin
			endtime:=endtimes;
			return endtime;
			end;
		function get_endtime return varchar2 is
			begin
			return endtime;
			end;
		function set_related_trans_scope(related_trans_scopes varchar2) return varchar2 is
			begin
			related_trans_scope:=related_trans_scopes;
			return related_trans_scope;
			end;
		function get_related_trans_scope return varchar2 is
			begin
			return related_trans_scope;
			end;
end vm_param;

3.执行创建视图,并调用vm_param的参数

create or replace view V_COR_SET_RPT_RELATED_NEW as
select t1.BOOK_DATE as print_date,
       t.cust_no,
       t.cust_name,
       t.related_trans_scope,
       t.cust_Belongs,
       t.owner_Property,
       t.merger_Situation,
       NVL(t1.deposit_balance, 0.00) as deposit_balance
  from cor_csm_base t
    left join (select 
    cust_no,
    BOOK_DATE,
    sum(a.BALANCE)
    from (select ct.cust_no,
    a.BOOK_DATE
     from COR_ACC_DAY_INFO a
     left join COR_ACC_ACNT_TYPE aat
     on aat.acnttype_code = a.acnttype_code
     left join COR_ACC_IACCOUNT ct
     on ct.acnt_no = a.acnt_no
    and aat.entity_code = 'C1'
    where a.ENTITY_CODE = 'C1'
    and  a.BOOK_DATE between vm_param.get_begintime and vm_param.get_endtime
    group by ct.cust_no, a.BOOK_DATE,ct.currency_code) ta
      group by ta.cust_no,ta.BOOK_DATE) t1
  on t.cust_no = t1.cust_no 

java代码 hibernate下的sql写法

 <select id="querySetRptRelDetailSize" resultClass="java.lang.Integer"
		parameterClass="java.util.HashMap">
		select 
		COUNT(cust_no)
	  	from V_COR_SET_RPT_RELATED_NEW
	  	where 1=1
	  	<isNotEmpty prepend="AND" property="printDate">
			vm_param.set_begintime ( #printDate# )= #printDate#
		</isNotEmpty>
		<isNotEmpty prepend="AND" property="printDate">
			vm_param.set_endtime ( #printDate# )= #printDate#
		</isNotEmpty>
	</select>

你可能感兴趣的:(oracle,数据库,sql)