[Oracle Dictionary] PL/SQL과 Table간의 Dependency 정보 확인

Oracle 2011. 2. 22. 17:52

PL/SQL을 이용하여 생성한 Package, Procedure, Function 등 다양한 OBJECTS와 Tables간의

Dependency 정보를 확인하기 위해서 어떻게 해야 할까??

개발할 때 마다, 이를 관리하기 위한 정보를 생성해??

아니다. Oracle Dictionary 중에서 "USER_DEPENDENCIES"를 조회하면 된다.

USER_OBJECTS나 USER_TABLES 정보와 잘 Join해서 검색한다면 보다 다양한 형태로 원하는 Dependency

정보를 확인해볼 수 있을 것입니다.

그러면 이쯤에서 하나 더....

Package나 Procedure, Function등을 개발하고, 이를 Input Parameter를 Application 개발자에게 문서로

전달해야 한다면 어떻게 할 것인가??

Source를 일일이 하나씩 열어서 문서로 옮겨 적어??     

Oh~~ No!!

눈치 빠른 분이라면 이미 아시겠지만, 이 또한 Dictionary 중에서 USER_ARGUMENTS 정보를 이용하면 된다.

Oracle로 개발하고, 이를 문서화하고자 한다면 일단 Dictionary에 있는 정보를 잘 참고해보도록 하자.

정말 존재하지 않는 Data를 문서화 하려고 한다면야 어쩔 수 없겠지만, 활용할 수 있는 정보들이 있음에도

불구하고 일일이 백지에 Typing으로 입력하려면 그야 말로 "머리가 나쁘면 손발이 고생한다"는 속담을

상기시켜주는 꼴이다.

제발 이렇게 시간 낭비하지 말자. 개발만 해도 부족한 시간 아니던가....



:     

TISTORY에 Login하려면 여기를 누르세요.


Oracle REF CURSOR

Oracle 2011. 2. 18. 10:45

With the REF_CURSOR you can return a recordset/cursor from a stored procedure.

There are 2 basic types: Strong ref cursor and weak ref cursor
For the strong ref cursor the returning columns with datatype and length need to be known at compile time.
For the weak ref cursor the structure does not need to be known at compile time.

The STRONG_REF_CURSOR and until Oracle 9i also the weak-type need to be declared in a package structure lik this:

create or replace
package REFCURSOR_PKG as
      TYPE WEAK8i_REF_CURSOR   IS REF CURSOR;
      TYPE STRONG_REF_CURSOR IS REF CURSOR RETURN EMP%ROWTYPE;
end REFCURSOR_PKG;

The pl/sql procedure that returns a ref-cursor looks like this:

/** until Oracle 9 */
create or replace
procedure test( p_deptno IN number,
                      p_cursor  OUT REFCURSOR_PKG.WEAK8i_REF_CURSOR)
is
begin
     open p_cursor FOR
     select *
     from   emp
     where  deptno = p_deptno;
end test;


Since Oracle 9i you can use SYS_REFCURSOR as the type for the returning REF_CURSOR.

/** From Oracle 9 */
create or replace
procedure test( p_deptno IN number,
                      p_cursor OUT SYS_REFCURSOR)
is
begin
      open p_cursor FOR
      select *
      from   emp
      where  deptno = p_deptno;
end test;


/* Strong type */
create or replace
procedure test( p_deptno  IN number,
                      p_cursor  OUT REFCURSOR_PKG.STRONG REF_CURSOR)
is
begin
      open p_cursor FOR
      select *
      from   emp
      where  deptno = p_deptno;
end test;

Selecting the ref_cursor from JDBC
To get the cursor from Java you can use the following JDBC-code:

public void method() throws SQLException{
  Connection conn = getConnection();
  CallableStatement cstmt = null;
  ResultSet rs = null;
  int deptno = 10;
  Object temp;
  try{
      cstmt = conn.prepareCall("begin  test(?,?); end;");
      cstmt.setInt(1, deptno);
      cstmt.registerOutParameter(2, OracleTypes.CURSOR);
      cstmt.execute();
      rs = (ResultSet) cstmt.getObject(2);
      ResultSetMetaData rsm = rs.getMetaData();
      int columnCount = rsm.getColumnCount();
      while (rs.next()){
         for (int j=0;j< columnCount;j++){
            temp = rs.getObject(j+1);
         }
      }
  } finally {
      if (!rs==null){
        rs.close();
      }
      if (!stmt==null){
        stmt.close();
      }
      if (!conn==null){
        conn.close();
      } 
  }
}
Calling ref-cursor from pl/sql
create or replace procedure test_call is
  c_cursor REFCURSOR_PKG.STRONG_REF_CURSOR;
  r_emp    c_emp%rowtype;
begin
  test(10,c_cursor);
  loop
    fetch c_cursor into r_emp;
    exit when c_cursor%notfound;
    dbms_output.put_line(r_emp.name);
  end loop;
  close c_cursor;
end test_call;

출처 : http://www.oradev.com/ref_cursor.jsp

 

:     

TISTORY에 Login하려면 여기를 누르세요.


[Oracle9i] Update구문의 Returning Clause

Oracle 2011. 2. 18. 10:31
Oracle9iR2 Documents에서 SQL Reference 문서를 찾은 후, UPDATE 구문에 대한 설명을

확인해보시면, Update 구문이 아래와 같은 구조를 같는다고 설명이 되어 있습니다.

오른쪽 하단에 보시면 Returning_Clause라고 존재하죠.


Text description of statements_1026.gif follows

하지만, 실제로는 많이 사용되지도 않을 뿐더러, 각 교육기관을 통해서 배울 때, 대부분 알고 있듯이

UPDATE table_name SET update_contents WHERE where_clause;

로만 보아왔고, 사용하셨을 것입니다.

Returning_Clause를 잘 사용하면 도움이 될 수 있으니 일단 알고 넘어가도록 하겠습니다.

      UPDATE employees
            SET job_id ='SA_MAN', salary = salary + 1000, department_id = 140
       WHERE last_name = 'Jones'
 RETURNING salary*0.25, last_name, department_id
           INTO :bnd1, :bnd2, :bnd3;

위의 SQL문을 확인해보면 아래와 같습니다.

last_name이 'Jones'인 사람을 찾아서 jod_id와 salary, department_id 값을 변경해주고,

bnd1, bnd2, bnd3의 변수에 salary*0.25, last_name, department_id 값을 Return 하도록 작성된

것입니다.

흔히 이와 같은 형태를 구현할 때, 아래와 같이 2개의 SQL문을 작성하여 처리하는 경우가 많습니다.

      UPDATE employees
            SET job_id ='SA_MAN', salary = salary + 1000, department_id = 140
       WHERE last_name = 'Jones';

      SELECT salary*0.25, last_name, department_id
           INTO :bnd1, :bnd2, :bnd
         FROM employees
       WHERE last_name = 'Jones'; 


하지만, 동일한 Table에 2번을 접근해야 한다는 점에서 비효율적이죠.

이럴 때, 앞서 제시한 예제 구문처럼 Returning Clause를 이용한다면 보다 효율적으로 작업을 진행할

수 있을듯 합니다.

단, 아래와 같은 경우에는 Returning Clause를 사용할 수 없으니 숙지하시기 바랍니다.

(생각해보면 당연하게 받아 들여질 수 있는 내용입니다.)

  • Specify the returning_clause for a multitable insert.
  • Use this clause with parallel DML or with remote objects.
  • Retrieve LONG types with this clause.
  • Specify this clause for a view on which an INSTEAD OF trigger has been defined.
 
:     

TISTORY에 Login하려면 여기를 누르세요.