通过Plsql查询获取表名

时间:2015-11-19 11:04:26

标签: sql database oracle plsql oracle11g

我需要一个输入user_name和password的过程然后通过每个表的创建脚本找到该模式中的现有表吗?

这是我试过的,但错了:

CREATE OR REPLACE procedure TABLE_INFO(P_USER_NAME IN VARCHAR2,P_PASSWORD IN VARCHAR,P_TABLE_NAME OUT VARCHAR2,P_SCRIPT OUT VARCHAR2) 
IS 
  chk_username all_users.username%type; 
  CURSOR C IS SELECT table_name FROM USER_TABLES; 
  t_tablename user_tables.table_name%type; 
BEGIN 
  SELECT username into chk_username from all_users 
  where chk_username=p_user_name; 
  open c; 
  loop 
    fetch c into t_tablename; 
    exit when c% notfound; 
    end loop; 
exception when no_data_found then 
  dbms_output.put_line('Wrong Username Or Password'); 
  close c; 
end; 
/

2 个答案:

答案 0 :(得分:0)

    Try this code. Let me know if this helps.

    CREATE OR REPLACE PROCEDURE TABLE_INFO(
        P_USER_NAME IN VARCHAR2,
        --    P_PASSWORD  IN VARCHAR, --Not required
        P_TABLE_NAME OUT sys_refcursor)
      --    P_SCRIPT OUT VARCHAR2) --Not required
    AS
    BEGIN

    -----------------------For DDL script use dbms_metadata.get_ddl-------------------------
      OPEN P_TABLE_NAME FOR 
      SELECT OWNER,TABLE_NAME,dbms_metadata.get_ddl('TABLE',TABLE_NAME) "table DDL" 
      FROM all_tables WHERE upper(OWNER) = upper(P_USER_NAME);

    END;

--------------------To EXECUTE this proc---------------------------------
var usr VARCHAR2(100);
VAR p_lst refcursor;
exec TABLE_INFO(<username>,:p_lst);
------------------------------------------------------------------------

答案 1 :(得分:0)

Check this out. This is a function returning all the values as per requirement.

--  Obj Creation
CREATE OR REPLACE TYPE my_obj
IS
  OBJECT
  (
    TAB_NAME VARCHAR2(100),
    DDL_S    VARCHAR2(32676) );

-- Table type Creation

CREATE OR REPLACE TYPE my_tab
IS
  TABLE OF my_obj;

--Function Creation---

CREATE OR REPLACE FUNCTION TABLE_INFO_FUN(
  P_USER_NAME IN VARCHAR2 )
RETURN my_tab
AS
  my_otpt my_tab;
BEGIN
  -----------------------For DDL script use dbms_metadata.get_ddl-------------------------
  SELECT CAST( multiset
    (SELECT my_obj(TABLE_NAME,dbms_metadata.get_ddl('TABLE',TABLE_NAME))
    FROM all_tables
    WHERE upper(OWNER) = upper(P_USER_NAME)
    ) AS my_tab )
  INTO my_otpt
  FROM DUAL;
  RETURN my_otpt;
END;

-- Using Function to Select Data -------------
SELECT * FROM TABLE(TABLE_INFO_FUN(<USERNAME>));