2008.10.08 15:31

[oracle]테이블 정의서를 작성하기 위한 프로시저

CREATE OR REPLACE PROCEDURE all_tabspec_procedure IS

 v_column_id   user_tab_columns.column_id%TYPE;
 v_column_name  user_tab_columns.column_name%TYPE;
 v_data_type   user_tab_columns.data_type%TYPE;
 v_data_length  user_tab_columns.data_length%TYPE;
 v_data_precision user_tab_columns.data_precision%TYPE;
 v_data_scale  user_tab_columns.data_scale%TYPE;  
 v_nullable   user_tab_columns.nullable%TYPE;
 v_data_default  user_tab_columns.data_default%TYPE;

 v_data_length_num VARCHAR2(10);
 


 CURSOR table_spec_cursor(in_table_name user_tab_columns.table_name%TYPE) IS

  select LTRIM(RTRIM(column_id)) NO,
      LTRIM(RTRIM(column_name)) ITEM_NAME, LTRIM(RTRIM(data_type)) 속성,
      LTRIM(RTRIM(data_length)) 길이, data_precision 길이_P,
      data_scale 길이_S, nullable 널, data_default 디폴트
  from user_tab_columns
  where table_name = in_table_name
  order by column_id;


 CURSOR all_object_cursor IS
--  select object_name from user_objects where object_type in('TABLE', 'VIEW') ;
--  select object_name from user_objects where object_type = 'TABLE' ;
--  select object_name from user_objects where object_type = 'VIEW' ;
  select distinct object_name from user_objects where object_name in ('ANNUITY', 'ANNUITY_DET', 'BANK', 'BASE_DET', 'BENEFIT_HIST',
    'CARE', 'CARE_DET', 'CHANGE', 'CHANGE_DET', 'COMN_KEEP', 'COUPL', 'FAMILY', 'GROW', 'GROW_DET',
    'LOAN', 'PAY_HIST', 'REFER', 'SPECIAL_CON_DET', 'TERM_DET');

BEGIN

 DBMS_OUTPUT.ENABLE(1000000);

 FOR allobj_cursor_rec IN all_object_cursor LOOP

  OPEN table_spec_cursor(allobj_cursor_rec.object_name);
 
  /* OBJECT NAME(TABLE OR VIEW) 인쇄*/
  DBMS_OUTPUT.PUT_LINE(' ');
  DBMS_OUTPUT.PUT_LINE(' ');
  DBMS_OUTPUT.PUT_LINE('====================   ' || allobj_cursor_rec.object_name || '   ====================');

  /* TYTLE 인쇄*/
  DBMS_OUTPUT.PUT_LINE('NO' || ',' || '   ' ||  'COLUMN_NAME' || ',' || '                      ' ||
          '속성' || ',' || '    ' || '길이' ||  ',' || '널'  ||  ',' || '       ' || '디폴트');

  LOOP
   FETCH table_spec_cursor
    INTO v_column_id, v_column_name, v_data_type, v_data_length, v_data_precision, v_data_scale, v_nullable, v_data_default;
   EXIT WHEN table_spec_cursor%NOTFOUND;

   /* (1)데이터Type이「NUMBER」인 경우는
     ① data_scale이0인 경우는data_precision을data_length로 지정    
                                              ② data_scale이NULL인 경우는data_length치를 그대로 사용
     ③ data_scale이0혹은NULL이 아닌 경우는「data_precision|data_scale」을data_length로 지정한다
        여기서는v_data_length_num변수를 사용(data_length는NUMBER TYPE)
     ④ data_scale이0이아닌 경우는data_precision을data_length로서 사용
      (2)데이터Type이「NUMBER」가 아닌 경우는data_length치를 그대로 사용
   IF v_data_type = 'NUMBER' THEN
    IF v_data_scale = 0 THEN
     v_data_length := v_data_precision;
     DBMS_OUTPUT.PUT_LINE(v_column_id || ',' || '   ' || RPAD(LTRIM(RTRIM(v_column_name)),30,' ') || ','|| '   ' ||
       RPAD(v_data_type,10,' ') || ',' || '   ' || v_data_length || ',' || '   ' ||
       RPAD(v_nullable,1,' ') || ',' || '   ' || LTRIM(RTRIM(v_data_default)));
    ELSE
     v_data_length_num := CONCAT(CONCAT(TO_CHAR(v_data_precision),'|'),TO_CHAR(v_data_scale));

     DBMS_OUTPUT.PUT_LINE(v_column_id || ',' || '   ' || RPAD(LTRIM(RTRIM(v_column_name)),30,' ') || ','|| '   ' ||
         RPAD(v_data_type,10,' ') || ',' || '   ' || v_data_length_num || ',' || '   ' ||
         RPAD(v_nullable,1,' ') || ',' || '   ' || LTRIM(RTRIM(v_data_default)));

    END IF;
   ELSE
    v_data_length := v_data_length;
    DBMS_OUTPUT.PUT_LINE(v_column_id || ',' || '   ' || RPAD(LTRIM(RTRIM(v_column_name)),30,' ') || ','|| '   ' ||
      RPAD(v_data_type,10,' ') || ',' || '   ' || v_data_length || ',' || '   ' ||
      RPAD(v_nullable,1,' ') || ',' || '   ' || LTRIM(RTRIM(v_data_default)));
   END IF;

  END LOOP;
  CLOSE table_spec_cursor;
   
 END LOOP;

END all_tabspec_procedure;
/

Trackback 0 Comment 0