2014년 9월 23일 화요일

Dynamic sql in Oracle 11g(DBMS_SQL)[오라클개발자교육/오라클/ORACLE강좌/오라클교육잘하는곳/오라클교육추천/ORACLE실무교육/ORACLE/ORACLE교육/ORACLE학원/오라클실무교육]

Dynamic sql in 11g(DBMS_SQL)

;’Oracle 11g Dynamic SQL 새 특징

-       CLOB에 의해 32k 보다 큰 SQL문장 허용(DBMS_SQL.PARSE)
-       REF CURSOR DBMS_SQL 커서로 변환이 가능
-       DBMS_SQL에서 모든 데이터 타입 지원
-       DBMS_SQL에서 사용자 정의 컬렉션 타입을 지원


[예제1] Dynamic SQL CLOB 컬럼에 저장되는 예제

SQL> set serveroutput on
SQL> DECLARE
  2
  3         v_sql    CLOB;
  4         v_cursor BINARY_INTEGER;
  5         v_dummy  INTEGER;
  6
  7      BEGIN
  8
  9       v_sql := 'DECLARE
 10                     v_variable VARCHAR2(10);
 11                  BEGIN
 12                     v_variable := ''Some Value'';';
 13
 14
 15       
 16        WHILE LENGTH(v_sql) <= 33000 LOOP
 17           v_sql := v_sql ||
 18                    ' /* comment || RPAD(''x'',4000,''x'') */ ' ||
 19                    CHR(10);
 20        END LOOP;
 21
 22       
 23        v_sql := v_sql ||
 24                 '   DBMS_OUTPUT.PUT_LINE(
 25                        ''Value of V_VARIABLE is ['' || v_variable || '']'');

 26                  END;';
 27
 28       
 29        DBMS_OUTPUT.PUT_LINE(
 30           'Length of CLOB is [' || LENGTH(v_sql) || ']');
 31
 32       
 33        v_cursor := DBMS_SQL.OPEN_CURSOR;
 34        DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.NATIVE);
 35        v_dummy := DBMS_SQL.EXECUTE(v_cursor);
 36        DBMS_SQL.CLOSE_CURSOR(v_cursor);
 37
 38     END;
 39     /
Length of CLOB is [33145]
Value of V_VARIABLE is [Some Value]

PL/SQL 처리가 정상적으로 완료되었습니다.



[예제2] DBMS_SQL ref_cursor 지원

SQL> DECLARE
  2         TYPE emp_type IS TABLE OF emp%ROWTYPE
  3            INDEX BY PLS_INTEGER;
  4
  5         v_emps    emp_type;
  6         v_sql     CLOB;
  7         v_cursor  BINARY_INTEGER := DBMS_SQL.OPEN_CURSOR;
  8         v_rc      SYS_REFCURSOR;
  9        v_execute BINARY_INTEGER;
 10   BEGIN
 11        v_sql := 'SELECT * FROM emp WHERE job = :job';
 12
 13       
 14        DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.NATIVE);
 15
 16       
 17        DBMS_SQL.BIND_VARIABLE(v_cursor, 'JOB', '&job');
 18
 19        /* 커서문 실행 및 REF CURSOR변환... */
 20        v_execute := DBMS_SQL.EXECUTE(v_cursor);
 21        v_rc := DBMS_SQL.TO_REFCURSOR(v_cursor);
 22
 23        LOOP
 24           FETCH v_rc BULK COLLECT INTO v_emps LIMIT 100;
 25           FOR i IN 1 .. v_emps.COUNT LOOP
 26              DBMS_OUTPUT.PUT_LINE('Emp = ' || v_emps(i).ename);
 27           END LOOP;
 28           EXIT WHEN v_rc%NOTFOUND;
 29        END LOOP;
 30        CLOSE v_rc;
 31
 32     END;
 33     /
job의 값을 입력하십시오: SALESMAN
  17:       DBMS_SQL.BIND_VARIABLE(v_cursor, 'JOB', '&job');
  17:       DBMS_SQL.BIND_VARIABLE(v_cursor, 'JOB', 'SALESMAN');
Emp = ALLEN
Emp = WARD
Emp = MARTIN
Emp = TURNER

PL/SQL 처리가 정상적으로 완료되었습니다.



 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… 오라클자바…12-271965
 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취…오라클자바…12-111445
53 [평일주간]100%환급6건,안드로이드,자바,C#,스프링3.2,SQL,힌트/… 오라클자바…03-151201
52 [주말주간]C#, ASP.NET마스터 오라클자바…01-311365
51 [평일,기업100%환급]SQL기초에서 Schema Object까지 오라클자바…01-311142
50 [평일야간]HTML5, CSS3,Ajax, jQuery마스터과정 오라클자바…01-311048
49 [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 오라클자바…01-191362
48 [평일주간,평일야간,주말]안드로이드개발자과정 오라클자바…01-111196
47 [평일야간,주말주간]JAVA,Network&JSP&Spring,MyBatis,Hibernate 오라클자바…01-031697
46 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… 오라클자바…12-271965
45 [평일야간,주말]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis… 오라클자바…12-191465
44 웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX,JavaScript) 오라클자바…12-141431
43 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… 오라클자바…12-111445
42 [평일,기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 오라클자바…12-091162
41 [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 오라클자바…12-011367
40 [기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍(평일주간(단기)… 오라클자바…12-011527
39 [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) 오라클자바…12-011025


댓글 없음:

댓글 쓰기