我有一个关于嵌套和左连接的大查询,并且Ineed创建了一个视图,以便不从应用程序运行它。问题是我需要日期范围和其他一些字段作为输入参数,因为它会因每个请求的前端而异。
我只是查了一下,看到一些帖子指的是使用SYS_CONTEXT进行参数化视图,并且需要确切地知道如何使用2个参数创建视图 - fromdate, todate
以及如何从应用程序调用视图。
仅供参考我使用grails / groovy开发应用程序。 这是我想要创建视图的查询..
select
d.dateInRange as dateval,
eventdesc,
nvl(td.dist_ucnt, 0) as dist_ucnt
from (
select
to_date(fromdate,'dd-mon-yyyy') + rownum - 1 as dateInRange
from all_objects
where rownum <= to_date(fromdate,'dd-mon-yyyy') - to_date(todate,'dd-mon-yyyy') + 1
) d
left join (
select
to_char(user_transaction.transdate,'dd-mon-yyyy') as currentdate,
count(distinct(grauser_id)) as dist_ucnt,
eventdesc
from
gratransaction, user_transaction
where gratransaction.id = user_transaction.trans_id and
user_transaction.transdate between to_date(fromdate,'dd-mon-yyyy') and to_date(todate,'dd-mon-yyyy')
group by to_char(user_transaction.transdate, 'dd-mon-yyyy'), eventdesc
) td on td.currentdate = d.dateInRange order by d.dateInRange asc
答案 0 :(得分:15)
此处描述了上下文方法:http://docs.oracle.com/cd/B28359_01/network.111/b28531/app_context.htm
e.g。 (例子改编自上面的链接)
CREATE CONTEXT dates_ctx USING set_dates_ctx_pkg;
CREATE OR REPLACE PACKAGE set_dates_ctx_pkg IS
PROCEDURE set(d1 in date, d2 in date);
END;
/
CREATE OR REPLACE PACKAGE BODY set_dates_ctx_pkg IS
PROCEDURE set(d1 in date, d2 in date) IS
BEGIN
DBMS_SESSION.SET_CONTEXT('dates_ctx', 'd1', TO_CHAR(d1,'DD-MON-YYYY'));
DBMS_SESSION.SET_CONTEXT('dates_ctx', 'd2', TO_CHAR(d2,'DD-MON-YYYY'));
END;
END;
/
然后,使用以下命令设置应用程序中的日期:
BEGIN set_dates_ctx_pkg.set(mydate1, mydate2); END;
/
然后,使用以下方法查询参数:
SELECT bla FROM mytable
WHERE mydate
BETWEEN TO_DATE(
SYS_CONTEXT('dates_ctx', 'd1')
,'DD-MON-YYYY')
AND TO_DATE(
SYS_CONTEXT('dates_ctx', 'd2')
,'DD-MON-YYYY');
这种方法的优点是它非常适合查询;它在运行时不涉及DDL或DML,因此没有任何事务需要担心;它非常快,因为它不涉及SQL-PL / SQL上下文切换。
可替换地:
如果您无法使用上下文方法和John的包变量方法,则另一种方法是将参数插入表中(例如,如果您正在运行查询中的全局临时表)同一会话),然后从视图加入到该表。缺点是您现在必须确保运行一些DML以在需要运行查询时插入参数。
答案 1 :(得分:6)
我刚刚为这个恼人的Oracle劣势做了一个解决方法。喜欢这个
create or replace package pkg_utl
as
type test_record is record (field1 number, field2 number, ret_prm_date date);
type test_table is table of test_record;
function get_test_table(prm_date date) return test_table pipelined;
end;
/
create or replace package body pkg_utl
as
function get_test_table(prm_date date) return test_table pipelined
is
begin
for item in (
select 1, 2, prm_date
from dual
) loop
pipe row (item);
end loop;
return;
end get_test_table;
end;
/
它仍然需要一个包,但至少我可以更方便地使用它:
select *
from table(pkg_utl.get_test_table(sysdate))
我不确定表现......
答案 2 :(得分:4)
要在视图中使用参数,一种方法是创建一个包,该包将设置参数的值并具有可以调用以获取这些值的函数。例如:
create or replace package MYVIEW_PKG as
procedure SET_VALUES(FROMDATE date, TODATE date);
function GET_FROMDATE
return date;
function GET_TODATE
return date;
end MYVIEW_PKG;
create or replace package body MYVIEW_PKG as
G_FROM_DATE date;
G_TO_DATE date;
procedure SET_VALUES(P_FROMDATE date, P_TODATE date) as
begin
G_FROM_DATE := P_FROMDATE;
G_TO_DATE := P_TODATE;
end;
function GET_FROMDATE
return date is
begin
return G_FROM_DATE;
end;
function GET_TODATE
return date is
begin
return G_TO_DATE;
end;
end MYVIEW_PKG;
然后可以创建您的视图:
create or replace view myview as
select
d.dateInRange as dateval,
eventdesc,
nvl(td.dist_ucnt, 0) as dist_ucnt
from (
select
MYVIEW_PKG.GET_FROMDATE + rownum - 1 as dateInRange
from all_objects
where rownum <= MYVIEW_PKG.GET_FROMDATE - MYVIEW_PKG.GET_TODATE + 1
) d
left join (
select
to_char(user_transaction.transdate,'dd-mon-yyyy') as currentdate,
count(distinct(grauser_id)) as dist_ucnt,
eventdesc
from
gratransaction, user_transaction
where gratransaction.id = user_transaction.trans_id and
user_transaction.transdate between MYVIEW_PKG.GET_FROMDATE and MYVIEW_PKG.GET_TODATE
group by to_char(user_transaction.transdate, 'dd-mon-yyyy'), eventdesc
) td on td.currentdate = d.dateInRange order by d.dateInRange asc;
要运行它,您必须先设置值:
exec MYVIEW_PKG.SET_VALUES(trunc(sysdate)-1,trunc(sysdate));
然后调用它将使用这些值:
select * from myview;