oracle创建带参数的视图
2017-07-28 1698
Oracle里面创建视图是比较容易的,但是如果视图需要动态传入参数,就没有办法直接实现了;
最近正好有类似的需求,通过查阅各种资料,发现可以用程序包的形式给视图传参
下面是具体的步骤
首先创建程序包
1 2 3 4 5 6 7 8 9 10 11 12 | --步骤1、 create or replace package p_view_param is --参数一 function set_param(num varchar2) return varchar2; function get_param return varchar2; --参数二 function set_Type(num varchar2) return varchar2; function get_Type return varchar2; --参数三 function set_Id(num varchar2) return varchar2; function get_Id return varchar2; end p_view_param; |
然后创建程序要的body:注意类型必须都一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | --步骤2、 create or replace package body p_view_param is paramParam varchar2(200); paramType varchar2(200); paramId varchar2(200); -- Param function set_param(num varchar2) return varchar2 is begin paramParam:=num; return num; end; function get_param return varchar2 is begin return paramParam; end; -- Type function set_Type(num varchar2) return varchar2 is begin paramType:=num; return num; end; function get_Type return varchar2 is begin return paramType; end; -- ID function set_Id(num varchar2) return varchar2 is begin paramId:=num; return num; end; function get_Id return varchar2 is begin return paramId; end; end p_view_param; |
最后创建视图
1 2 3 4 5 | CREATE OR REPLACE VIEW view_yuebao(dh,xymc,xyzt,dbilldate) AS SELECT dh,xymc,xyzt,dbilldate FROM hq_studentrecords WHERE SUBSTR(dbilldate,1,10) >= p_view_param.get_Id AND SUBSTR(dbilldate,1,10) <= p_view_param.get_Type AND xyzt = p_view_param.get_param |
现在就可以给视图传参数进行查询了
1 2 | SELECT * FROM view_yuebao a WHERE p_view_param.set_Id('2017-06-22') = '2017-06-22' AND p_view_param.set_Type('2017-06-25') = '2017-06-25' AND p_view_param.set_param('1') ='1' |
至此,一个可以传参数的视图就创建完毕
下一篇:JS基础知识点整理