3,499
社区成员
发帖
与我相关
我的任务
分享 select * from user.emp
where (V_empid=0 or V_empid = empid)
and (V_empname='' or V_empname = empname)
and (V_empaddr=0 or V_empaddr = empaddr)
and (V_empbrithday='' or V_empbrithday = empbrithday)
;
if V_empid0 then
select * from user.emp
where (V_empid=0 )
and (V_empname='' or V_empname = empname)
and (V_empaddr=0 or V_empaddr = empaddr)
and (V_empbrithday='' or V_empbrithday = empbrithday) ;
end if;
--V_LIST是一个数组
if v_empid<>0 then
V_LIST.EXTEND();
V_LIST(V_LIST.COUNT) := v_empid ;
v_sql = 'and empid=:1'
end if;
if v_empaddr<>'' then
V_LIST.EXTEND();
V_LIST(V_LIST.COUNT) := UPPER(v_empaddr);
v_sql=v_sql||' and empaddr=:2'
end if;
V_SQL:='select * from user.emp where 1=1 '
CASE V_LIST.COUNT
WHEN 0 THEN
EXECUTE IMMEDIATE V_SQL;
WHEN 1 THEN
EXECUTE IMMEDIATE V_SQL
USING V_STRLIST(1);
WHEN 2 THEN
EXECUTE IMMEDIATE V_SQL
USING V_LIST(1), V_LIST(2);
end case;
CREATE OR REPLACE PROCEDURE query_employees
(hiredate hr.employees.hire_date%TYPE := null,
jobid hr.employees.job_id%TYPE := null,
salary hr.employees.salary%TYPE := null,
empcur OUT SYS_REFCURSOR)
IS
stmt varchar2(4000);
curid integer;
ret integer;
BEGIN
stmt := 'select * from hr.employees where 1=1 '||
(case when hiredate is not null then 'and hire_date<:h ' end)||
(case when jobid is not null then 'and job_id=:j' end)||
(case when salary is not null then 'and salary>:s' end);
curid := dbms_sql.open_cursor;
dbms_sql.parse(curid, stmt, dbms_sql.native);
IF hiredate is not null THEN
dbms_sql.bind_variable(curid,'h',hiredate);
END IF;
IF jobid is not null THEN
dbms_sql.bind_variable(curid,'j',jobid);
END IF;
IF salary is not null THEN
dbms_sql.bind_variable(curid,'s',salary);
END IF;
ret := dbms_sql.execute(curid);
-- Convert the cursor number to the cursor variable (oracle 11g).
empcur := dbms_sql.to_refcursor(curid);
END; /
SQL> explain plan for select * from emp where 0=7369 or empno=7369;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 212296133
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 1 (0)| 00:0
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 1 (0)| 00:0
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| 00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("EMPNO"=7369)
14 rows selected
SQL> SQL> set autotrace on
SQL> select * from emp where 0=7369 or empno=7369;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-12月-80 800.1
20
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=37)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=1 Car
d=1 Bytes=37)
2 1 INDEX (UNIQUE SCAN) OF 'PK_EMP' (INDEX (UNIQUE)) (Cost=0
Card=1)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
810 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed