2014-11-24 79 views
-1

需要關於PL/SQL代碼的幫助...我需要查看有關用戶希望檢查的模式中每個表的信息,但是在我的結果視圖中,表也會出現,我不需要它們... 我只需要在例如列表的表和字段:表1第1列,表1第2列等 這裏是代碼:使用plsql獲取表信息

CREATE OR replace PROCEDURE table_info_proc(p_schema_name IN VARCHAR2) 
IS 
TYPE all_tab_columns_type 
IS 
    TABLE OF all_tab_columns%ROWTYPE INDEX BY PLS_INTEGER; 
    v_all_tab ALL_TAB_COLUMNS_TYPE; 
    l_row PLS_INTEGER; 
    v_schema  VARCHAR2(50); 
    v_table_count NUMBER(5); 
BEGIN 
    SELECT DISTINCT(owner) 
    INTO   v_schema 
    FROM   all_objects 
    WHERE   owner = p_schema_name; 

    dbms_output.Put_line ('SCHEMA NAME: ' 
    || v_schema); 
    SELECT * bulk collect 
    INTO v_all_tab 
    FROM all_tab_columns 
    WHERE owner = p_schema_name; 

    l_row := v_all_tab.first; 
    WHILE (l_row IS NOT NULL) 
    LOOP 
    dbms_output.Put_line ('table: ' 
    || V_all_tab(l_row).table_name 
    || ' column: ' 
    || V_all_tab(l_row).column_name); 
    l_row := v_all_tab.NEXT(l_row); 
    END LOOP; 
    SELECT count(*) 
    INTO v_table_count 
    FROM all_objects 
    WHERE object_type = 'TABLE' 
    AND owner = p_schema_name; 

    dbms_output.Put_line ('NUMBER OF TABLES IN SCHEMA: ' 
    || v_table_count); 
EXCEPTION 
WHEN no_data_found THEN 
    dbms_output.Put_line ('There is no schema named ' 
    || p_schema_name); 
END table_info_proc; 
/

回答

0

你可以在普通SQL做到這一點,沒有必要的PL/SQL

  • ALL_TAB_COLUMNS描述了當前用戶可訪問的表,視圖和 羣集的列。
  • DBA_TAB_COLUMNS描述數據庫中所有表,視圖和 羣集的列。
  • USER_TAB_COLUMNS描述當前用戶擁有的表,視圖和 羣集的列。此視圖不顯示 OWNER列。

*_TAB_COLS

SQL> SELECT table_name, column_name 
    2 FROM all_tab_cols 
    3 WHERE owner='SCOTT' 
    4/

TABLE_NAME  COLUMN_NAME 
--------------- --------------- 
TABLE_NAME  DEPTNO 
TABLE_NAME  ENAME 
TABLE_NAME  EMPNO 
SALGRADE  HISAL 
SALGRADE  LOSAL 
SALGRADE  GRADE 
EMP    SYS_STSA11ZDTGW 
       $SYV6W40D3EV5X5 

EMP    DEPTNO 
EMP    COMM 
EMP    SAL 
EMP    HIREDATE 
EMP    MGR 
EMP    JOB 
EMP    ENAME 
EMP    EMPNO 
DEPT   LOC 
DEPT   DNAME 
DEPT   DEPTNO 
BONUS   COMM 
BONUS   SAL 
BONUS   JOB 
BONUS   ENAME 

22 rows selected. 

SQL> 

編輯如果你還是想用PL/SQL,然後,

SQL> BEGIN 
    2 FOR i IN 
    3 (SELECT table_name, column_name FROM all_tab_cols WHERE owner='SCOTT' 
    4 ) 
    5 LOOP 
    6  dbms_output.put_line('TABLE_NAME = '||i.table_name||' COLUMN_NAME = '||i.column_name); 
    7 END LOOP; 
    8 END; 
    9/
TABLE_NAME = TABLE_NAME COLUMN_NAME = DEPTNO 
TABLE_NAME = TABLE_NAME COLUMN_NAME = ENAME 
TABLE_NAME = TABLE_NAME COLUMN_NAME = EMPNO 
TABLE_NAME = SALGRADE COLUMN_NAME = HISAL 
TABLE_NAME = SALGRADE COLUMN_NAME = LOSAL 
TABLE_NAME = SALGRADE COLUMN_NAME = GRADE 
TABLE_NAME = EMP COLUMN_NAME = SYS_STSA11ZDTGW$SYV6W40D3EV5X5 
TABLE_NAME = EMP COLUMN_NAME = DEPTNO 
TABLE_NAME = EMP COLUMN_NAME = COMM 
TABLE_NAME = EMP COLUMN_NAME = SAL 
TABLE_NAME = EMP COLUMN_NAME = HIREDATE 
TABLE_NAME = EMP COLUMN_NAME = MGR 
TABLE_NAME = EMP COLUMN_NAME = JOB 
TABLE_NAME = EMP COLUMN_NAME = ENAME 
TABLE_NAME = EMP COLUMN_NAME = EMPNO 
TABLE_NAME = DEPT COLUMN_NAME = LOC 
TABLE_NAME = DEPT COLUMN_NAME = DNAME 
TABLE_NAME = DEPT COLUMN_NAME = DEPTNO 
TABLE_NAME = BONUS COLUMN_NAME = COMM 
TABLE_NAME = BONUS COLUMN_NAME = SAL 
TABLE_NAME = BONUS COLUMN_NAME = JOB 
TABLE_NAME = BONUS COLUMN_NAME = ENAME 

PL/SQL procedure successfully completed. 

SQL> 

EDIT 2如果你想輸出僅限制表,那麼,

SELECT a.table_name, 
    a.column_name, 
    c.object_type 
FROM all_tab_cols A, 
    all_tables b, 
    all_objects c 
WHERE A.table_name = b.table_name 
AND b.table_name = c.object_name 
AND A.owner  ='SCOTT' 
/
+0

是的,但我需要在PLSQL中完成,我無法弄清楚它... – 2014-11-24 09:03:37

+0

好的,請參閱我的編輯。 – 2014-11-24 09:08:32

+0

thx,但仍然不好,它列出視圖以及...我粘貼的解決方案獲得相同的結果... – 2014-11-24 09:15:57