17,086
社区成员
发帖
与我相关
我的任务
分享
string sql = "select * from tb1 where 1=1 ";
if(id != null){
sql += "and id = :vId ";
}
if(name != null){
sql += "and name = :vName ";
}
CREATE OR REPLACE PROCEDURE P_GetData(vId in varchar2,vName in varchar2) IS
vSql Varchar2(1000);
BEGIN
vSql := 'select * from tb1 where 1=1 ';
if vId is not null then
vSql := vSql || 'and id = ' || vId || '';
end if;
if vName is not null then
vSql := vSql || 'and name = ' || vName || '';
end if;
execute immediate vSql;
END P_GetData;
CREATE OR REPLACE PROCEDURE P_GetData(vId in varchar2,vName in varchar2) IS
tmp renyb%ROWTYPE;
BEGIN
if vId is not null and vName is not null then
select * into tmp from renyb where id = vId and name = vName;
else
if vId is not null then
select * into tmp from renyb where id = vId;
end if;
if vName is not null then
select * into tmp from renyb where name = vName;
end if;
end if;
END P_GetData;
string sql = "select * from tb1 where 1=1 ";
if(id != null){
sql += "and id = :vId ";
}
if(name != null){
sql += "and name = :vName ";
}
string sql = "select * from tb1 where 1=1 ";
if(id != null){
sql += "and id = :vId ";
}
if(name != null){
sql += "and name = :vName ";
}
CREATE OR REPLACE PROCEDURE P_GetData(v_instr varchar2) IS
vSql Varchar2(1000);
emptype emp%rowtype;
BEGIN
vSql := 'select * from emp where '||v_instr;
execute immediate vSql into emptype;
dbms_output.put_line(emptype.empno||emptype.ename);
END P_GetData;
SQL> exec P_GetData('empno=1 and deptno=20 ');
1SMITH
PL/SQL procedure successfully completed
SQL> create index i1 on emp(ename);
Index created.
SQL> select /*+ index(emp i1)*/ empno,ename from emp where (ename=:ename or :ename is null);
EMPNO ENAME
---------- ----------
7902 FORD
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 20 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 2 | 20 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(:ENAME IS NULL OR "ENAME"=:ENAME)
SQL> select /*+rule*/ empno,ename from emp where (ename=:ename or :ename is null);
EMPNO ENAME
---------- ----------
7902 FORD
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
----------------------------------
| Id | Operation | Name |
----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | TABLE ACCESS FULL| EMP |
----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ENAME"=:ENAME OR :ENAME IS NULL)
Note
-----
- rule based optimizer used (consider using cbo)
SQL> select empno,ename from emp where ename=:ename;
EMPNO ENAME
---------- ----------
7902 FORD
Execution Plan
----------------------------------------------------------
Plan hash value: 727703203
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 10 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I1 | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ENAME"=:ENAME)
scott@ORA1> select * from emp where ename=:x OR :x is null;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
执行计划
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 74 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 2 | 74 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(:X IS NULL OR "ENAME"=:X)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
718 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
scott@ORA1> select * from emp where ename=:x;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
执行计划
----------------------------------------------------------
Plan hash value: 79656022
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_EMP_NAME | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ENAME"=:X)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
722 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed