PLSQL_动态语句的解析(概念)

2014-06-02 Created By BaoXinjian

1. 最简单例子


(1).SQL 

 1 DECLARE

 3     l_sql_text VARCHAR (1000); 

 5     TYPE c_type_wip_entity IS REF CURSOR; 

 7  c_wip_entity c_type_wip_entity; 

 9     r_wip_entity wip_entities%ROWTYPE; 

11 BEGIN 

13     l_sql_text :='select * from wip_entities' 

15                    || ' where wip_entity_id in ('|| ''''|| '2363' || ''','' '|| '2462'|| ''')'; 16 

17  DBMS_OUTPUT.put_line (l_sql_text);

19     OPEN c_wip_entity FOR l_sql_text; 

21  LOOP 

23    FETCH c_wip_entity INTO r_wip_entity; 

25         EXIT WHEN c_wip_entity%NOTFOUND; 

27         DBMS_OUTPUT.put_line ('Job Name-->' || r_wip_entity.wip_entity_name); 

29     END LOOP; 

31 END;

(2).DBMS Output

 

 

2. 动态语句结合批处理


(1).SQL 

 1 DECLARE 

 3     i NUMBER; 

 5     l_sql_text VARCHAR (1000); 

 7     TYPE c_type_wip_entity IS TABLE OF wip_entities%ROWTYPE; 

 9  c_wip_entity c_type_wip_entity; 

11     p_wip_entity_id NUMBER := 2363; 

13 BEGIN 

15     l_sql_text := 'select * from wip_entities where wip_entity_id = :wip_entity_id'; 

17     EXECUTE IMMEDIATE l_sql_text 

19     BULK COLLECT INTO c_wip_entity 

21  USING p_wip_entity_id; 

23     FOR i IN 1 .. c_wip_entity.COUNT 

25  LOOP 

27  DBMS_OUTPUT.put_line (c_wip_entity (i).wip_entity_name); 

29     END LOOP;

31 END;

(2).DBMS Output

 

 

3. 动态更新语句


(1).SQL 

 1 DECLARE 

 3     l_sql_text VARCHAR (1000); 

 5 BEGIN 

 7     l_sql_text := 'update cux_wf_demo_documents set note= ' || '''Test''' || ' where document_id= :document_id'; 

 9     EXECUTE IMMEDIATE l_sql_text 

11         USING 1; 

13  DBMS_OUTPUT.put_line (l_sql_text); 

15 END;

(2).DBMS Output

 

 

Thanks and Regarads

你可能感兴趣的:(plsql)