2014-01-29 63 views
-3

如何根據if else語句在oracle中創建動態sql。Oracle中的動態sql

sql:= 'select n from where a = p_a'; 
if p_b is not null then 
sql:= 'select n from where a = p_a and b = p_b';  
+0

的'執行immediate'聽到這個條件來構建你的命令? – Rachcha

回答

2

爲什麼只要執行以下操作即可獲得動態SQL?

select n 
from xx 
where a = p_a and (b = p_b or p_b is null); 
+0

@Gondon感謝您的幫助,但需要我concatane sql query.This查詢不起作用。 –

+0

@GoranZooferic它適合你嗎?你得到什麼錯誤? –

+0

Maheswaran請原諒我。我不再看。我找到不同的解決方案。再次感謝。 –

1

例子:

CREATE TYPE t_emp AS OBJECT (id NUMBER, name VARCHAR2(20)) 
/
CREATE TYPE t_emplist AS TABLE OF t_emp 
/

CREATE TABLE dept_new (id NUMBER, emps t_emplist) 
    NESTED TABLE emps STORE AS emp_table; 

INSERT INTO dept_new VALUES (
    10, 
    t_emplist(
     t_emp(1, 'SCOTT'), 
     t_emp(2, 'BRUCE'))); 

DECLARE 
    deptid NUMBER; 
    ename VARCHAR2(20); 
BEGIN 
    EXECUTE IMMEDIATE 'SELECT d.id, e.name 
     FROM dept_new d, TABLE(d.emps) e -- not allowed in static SQL 
              -- in PL/SQL 
     WHERE e.id = 1' 
     INTO deptid, ename; 
END; 
/

您可以將您自己的邏輯根據要 Reference