Oracle 由和(1=1或:edate为空)完成。通过这种方式,您可以利用OMG小马的“上下文变量”(在where子句中只有“有意义的”条件),而不需要它们的复杂性和创建上下文的必要性。在某些情况下,对于长时间运行的查询,完全消除绑定会更有效。但是这种情况很少见,如果
Oracle 由和(1=1或:edate为空)完成。通过这种方式,您可以利用OMG小马的“上下文变量”(在where子句中只有“有意义的”条件),而不需要它们的复杂性和创建上下文的必要性。在某些情况下,对于长时间运行的查询,完全消除绑定会更有效。但是这种情况很少见,如果,oracle,plsql,oracle10g,dynamic-sql,Oracle,Plsql,Oracle10g,Dynamic Sql,由和(1=1或:edate为空)完成。通过这种方式,您可以利用OMG小马的“上下文变量”(在where子句中只有“有意义的”条件),而不需要它们的复杂性和创建上下文的必要性。在某些情况下,对于长时间运行的查询,完全消除绑定会更有效。但是这种情况很少见,如果你不小心的话,你很容易受到SQL注入的攻击。我还没有听说过上下文变量,我认为它们可能对我正在研究的其他东西很有用(感谢你让我注意到它们!),但我仍然不清楚为什么在这种情况下它们比绑定变量更好。作为一个简单的例子,如果我的查询是:从CQV q中选
由
和(1=1或:edate为空)
完成。通过这种方式,您可以利用OMG小马的“上下文变量”(在where子句中只有“有意义的”条件),而不需要它们的复杂性和创建上下文的必要性。在某些情况下,对于长时间运行的查询,完全消除绑定会更有效。但是这种情况很少见,如果你不小心的话,你很容易受到SQL注入的攻击。我还没有听说过上下文变量,我认为它们可能对我正在研究的其他东西很有用(感谢你让我注意到它们!),但我仍然不清楚为什么在这种情况下它们比绑定变量更好。作为一个简单的例子,如果我的查询是:从CQV q中选择*其中q.bcode=sys_context('ctx1','bcode')与从CQV q中选择*其中q.bcode=:bcode,那么上下文变量方法比绑定变量有什么优势?区别在于当使用绑定变量时,查询中始终必须有bind变量引用。IE:如果值为null,则必须有和:bcode为null
。使用上下文变量意味着不必包含无关/冗余的WHERE
clausesha!我现在看到了!打开游标的代码取决于游标中有多少绑定变量,但是否使用上下文变量无关紧要!:)嗯,现在我需要和DBA谈谈,给我执行dbms_会话的权限。设置_上下文…@沮丧:一直在那里-我提供的信息&这两个链接过去对我有用:)
select num
from (select distinct q.num
from cqqv q
where q.bcode = '1234567' --this is variable
and q.lb = 'AXCT' --this is variable
and q.type = 'privt' --this is variable
and q.edate > sysdate - 30 --this is variable
order by dbms_random.value()) subq
where rownum <= 10; --this is variable
select num
from (select distinct q.num
from cqqv q
where q.bcode = '1234567' --this is variable
and q.lb = 'AXCT' --this is variable
--and q.type = 'privt' --this condition ignored because of "type=*" in input
and q.edate > sysdate - 30 --this is variable
order by dbms_random.value()) subq
where rownum <= 10; --this is variable
select num
from (select distinct q.num
from cqqv q
where 1=1
and (:bcode is null or q.bcode = :bcode)
and (:lb is null or q.lb = :lb)
and (:type is null or q.type = :type)
and (:edate is null or q.edate > :edate - 30)
order by dbms_random.value()) subq
where rownum <= :numrows
declare
rc sys_refcursor;
q long;
begin
q := 'select num
from (select distinct q.num
from cqqv q
where 1=1';
if p_bcode is not null then
q := q || 'and q.bcode = :bcode';
else
q := q || 'and (1=1 or :bcode is null)';
end if;
if p_lb is not null then
q := q || 'and q.lb = :lb';
else
q := q || 'and (1=1 or :lb is null)';
end if;
if p_type is not null then
q := q || 'and q.type = :type';
else
q := q || 'and (1=1 or :type is null)';
end if;
if p_edate is not null then
q := q || 'and q.edate = :edate';
else
q := q || 'and (1=1 or :edate is null)';
end if;
q := q || ' order by dbms_random.value()) subq
where rownum <= :numrows';
open rc for q using p_bcode, p_lb, p_type, p_edate, p_numrows;
return rc;
end;
select num
from (select distinct q.num
from cqqv q
where 1=1
and q.bcode = :bcode
and q.lb = :lb
and (1=1 or :type is null)
and (1=1 or :edate is null)
order by dbms_random.value()) subq
where rownum <= :numrows
and (:bcode is null or q.bcode = :bcode)
L_CURSOR SYS_REFCURSOR;
L_QUERY VARCHAR2(5000) DEFAULT 'SELECT num
FROM (SELECT DISTINCT q.num
FROM CQQV q
WHERE 1 = 1 ';
BEGIN
IF IN_BCODE IS NOT NULL THEN
DBMS_SESSION.SET_CONTEXT('THE_CTX',
'BCODE',
IN_BCODE);
L_QUERY := L_QUERY || ' AND q.bcode = SYS_CONTEXT(''THE_CTX'', ''BCODE'') ';
END IF;
IF IN_LB IS NOT NULL THEN
DBMS_SESSION.SET_CONTEXT('THE_CTX',
'LB',
IN_LB);
L_QUERY := L_QUERY || ' AND q.lb = SYS_CONTEXT(''THE_CTX'', ''LB'') ';
END IF;
IF IN_TYPE IS NOT NULL THEN
DBMS_SESSION.SET_CONTEXT('THE_CTX',
'TYPE',
IN_TYPE);
L_QUERY := L_QUERY || ' AND q.type = SYS_CONTEXT(''THE_CTX'', ''TYPE'') ';
END IF;
IF IN_EDATE IS NOT NULL THEN
DBMS_SESSION.SET_CONTEXT('THE_CTX',
'EDATE',
IN_EDATE);
L_QUERY := L_QUERY || ' AND q.edate = SYS_CONTEXT(''THE_CTX'', ''EDATE'') - 30 ';
END IF;
L_QUERY := L_QUERY || ' ORDER BY dbms_random.value()) subq
WHERE rownum <= :numrows ';
FOR I IN 0 .. (TRUNC(LENGTH(L_QUERY) / 255)) LOOP
DBMS_OUTPUT.PUT_LINE(SUBSTR(L_QUERY, I * 255 + 1, 255));
END LOOP;
OPEN L_CURSOR FOR L_QUERY USING IN_ROWNUM;
RETURN L_CURSOR;
END;
DBMS_SESSION.SET_CONTEXT('THE_CTX', 'LB', IN_LB);
... L_QUERY USING IN_EXAMPLE_VALUE
select num
from (select distinct q.NUM
from cqqv q
where (q.bcode = :bcode)
and (1=1 or :lb is null)
and (1=1 or :type is null)
and (q.edate> :edate)
order by dbms_random.value()) subq
where rownum <= :numrows
select num
from (select distinct q.num
from cqqv q
where q.bcode = '1234567' --this is variable
and q.lb = 'AXCT' --this is variable
and q.type = nvl(<variable-type>, q.type) --this condition ignored because of "type=*" in input
and q.edate > sysdate - 30 --this is variable
order by dbms_random.value()) subq
where rownum <= 10; --this is variable