-1

我需要一个单一的 sql 查询,它将从模式中检索我所有的元数据信息。我们将无法在此处创建数据库链接。因此,需要将两个模式中的元数据转储到 2 个不同的表中,然后比较模式

4

1 回答 1

0

我以前为别人做过。如果您喜欢它,请随意使用它。

CREATE PROCEDURE copy_prc
AS
BEGIN
    NULL;
END;


CREATE FUNCTION copy_fun RETURN BOOLEAN
AS
BEGIN
    RETURN TRUE;
END;


CREATE PACKAGE copy_pkg AS
    PROCEDURE pkg_prc;
END copy_pkg;


CREATE PACKAGE BODY copy_pkg AS
    PROCEDURE pkg_prc
    AS
    BEGIN
        NULL;
    END;
END copy_pkg;


CREATE TABLE source_codes
(
    s_database     VARCHAR2(30)
,   s_package_name VARCHAR2(30)
,   s_source_code  CLOB
);


DECLARE
    CURSOR objects_cur IS
        SELECT  object_name, DBMS_METADATA.GET_DDL(object_type, object_name) r_source_code
        FROM    dba_objects
        WHERE   owner       =  USER
        AND     object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE')
        AND     object_name IN ('COPY_PRC', 'COPY_FUN', 'COPY_PKG');

    TYPE objects_ntt IS TABLE OF objects_cur%ROWTYPE;

    l_objects  objects_ntt;
    l_database_name  VARCHAR2(100);
BEGIN
    OPEN  objects_cur;
    FETCH objects_cur BULK COLLECT INTO l_objects;
    CLOSE objects_cur;

    l_database_name := sys_context('USERENV', 'DB_NAME');

    FORALL indx IN 1..l_objects.COUNT
        INSERT INTO source_codes
        (
            s_database
        ,   s_package_name
        ,   s_source_code
        )
        VALUES
        (
            l_database_name
        ,   l_objects(indx).object_name
        ,   l_objects(indx).r_source_code
        );

    COMMIT;
END;

SELECT  s_database, s_package_name, DBMS_LOB.SUBSTR(s_source_code, 40)
FROM    source_codes;
/*
my_db_name COPY_FUN "  CREATE OR REPLACE FUNCTION "SCHEMA_NAME".""
my_db_name COPY_PKG "  CREATE OR REPLACE PACKAGE "SCHEMA_NAME"."C"
my_db_name COPY_PRC "  CREATE OR REPLACE PROCEDURE "SCHEMA_NAME"."
*/
于 2013-06-19T14:48:34.497 回答