Oracle - 更改多个对象的变量值

时间:2017-12-04 21:29:14

标签: plsql triggers procedure

我想知道是否可以编写一个脚本来更改多个对象中变量的值。

我运行了以下查询以获取需要更改的对象列表:

select distinct owner, name, type from dba_source
where lower(text) like '%environment_v%';

我是否可以编写一个脚本来将environment_v的值从10改为20到所有这些对象?

感谢

1 个答案:

答案 0 :(得分:0)

这是一个你几乎肯定不想完全自动化的问题。下面的代码将找到相关对象并返回其DDL。但是,应该手动完成更改代码和运行代码。

正确解析和更改代码是一个难题。您正在使用的搜索字符串在12.2上的SYS模式中返回许多误报。 - 您可能不想更改这些对象。

--Create DDL statements that contain "%environment_v%".
--These statements must be modified to include the new code.
select owner, object_type, object_name, dbms_metadata.get_ddl(object_type, object_name, owner)
from
(
    --Convert DBA_OBJECTS.OBJECT_TYPE to DBMS_METADATA object type:
    select
        owner,
        --Java object names may need to be converted with DBMS_JAVA.LONGNAME.
        --That code is not included since many database don't have Java installed.
        object_name,
        decode(object_type,
            'DATABASE LINK',      'DB_LINK',
            'JOB',                'PROCOBJ',
            'RULE SET',           'PROCOBJ',
            'RULE',               'PROCOBJ',
            'EVALUATION CONTEXT', 'PROCOBJ',
            'PACKAGE',            'PACKAGE_SPEC',
            'PACKAGE BODY',       'PACKAGE_BODY',
            'TYPE',               'TYPE_SPEC',
            'TYPE BODY',          'TYPE_BODY',
            'MATERIALIZED VIEW',  'MATERIALIZED_VIEW',
            'QUEUE',              'AQ_QUEUE',
            'JAVA CLASS',         'JAVA_CLASS',
            'JAVA TYPE',          'JAVA_TYPE',
            'JAVA SOURCE',        'JAVA_SOURCE',
            'JAVA RESOURCE',      'JAVA_RESOURCE',
            object_type
        ) object_type
    from dba_objects 
    where
        --These objects are included with other object types.
        object_type not in ('INDEX PARTITION','INDEX SUBPARTITION',
           'LOB','LOB PARTITION','TABLE PARTITION','TABLE SUBPARTITION')
        --Ignore system-generated types that support collection processing.
        and not (object_type = 'TYPE' and object_name like 'SYS_PLSQL_%')
        --Exclude nested tables, their DDL is part of their parent table.
        and (owner, object_name) not in (select owner, table_name from dba_nested_tables)
        --Exclude overflow segments, their DDL is part of their parent table.
        and (owner, object_name) not in (select owner, table_name from dba_tables where iot_type = 'IOT_OVERFLOW')
        --Only look for objects with a specific variable.
        and (owner, object_name, object_type) in
        (
            select owner, name, type
            from dba_source
            where lower(text) like '%environment_v%'
        )
)
order by owner, object_type, object_name;

以上查询基于this answer。这个答案已被编辑了10次,我已经在数千个对象上使用它,所以它应该是生成DDL的一个很好的起点。

理想情况下,您可以在版本控制的文本文件中进行这些更改,而不是直接在数据库中进行。