使用plsql获取表信息

时间:2014-11-24 08:53:02

标签: sql oracle plsql

需要有关PL / SQL代码的帮助...我需要查看用户希望检查的架构中每个表的信息,但在我的结果视图表中也会出现,我不需要它们...... 我只需要列表中的表及其列,例如:表1列1,表1列2等。 这是代码:

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; 
/

1 个答案:

答案 0 :(得分: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>

编辑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'
/