创建一个包含2列的集合,并在PL / SQL中一次插入所有行

时间:2017-09-08 21:19:12

标签: oracle plsql

我有参考数据,我想在PL / SQL包中使用。 这是一个包含两列的集合:

type table_info IS RECORD (
table_name VARCHAR2(50),
join_column VARCHAR2(50)
);

type config_tables_type is table of table_info; -- list of the config tables

我想一次向这个集合添加几行,我试过这个:

config_tables config_tables_type := config_tables_type (table_info('Commands','object_id'),
                                                           table_info('Contact_notificationcommands','command_object_id'),
                                                           table_info('Contactgroup_members','contact_object_id'),
                                                           table_info('Contactgroups','contact_object_id'),
                                                           table_info('Contactnotificationmethods','command_object_id'),
                                                           table_info('customvariables','object_id'),
                                                           table_info('Host_contactgroups','host_id'),
                                                           table_info('Host_contacts','host_id'),
                                                           table_info('Hostescalation_contactgroups','contactgroup_object_id'),
                                                           table_info('Hostescalation_contacts','contact_object_id'),                                                          
                                                           table_info('Host_parenthosts','parent_host_object_id'),
                                                           table_info('Hostdependencies','host_object_id'),
                                                           table_info('Hostdependencies','dependent_host_object_id'),
                                                           table_info('Hostescalations','host_object_id'),
                                                           table_info('Hostgroup_members','host_object_id'),
                                                           table_info('Hostgroups','hostgroup_object_id'),
                                                           table_info('Hosts','host_object_id'),
                                                           table_info('Service_contactgroups','contactgroup_object_id'),
                                                           table_info('Service_contacts','contact_object_id'),
                                                           table_info('Servicedependencies','service_object_id'),
                                                           table_info('Serviceescalation_contactgroups','contactgroup_object_id'),
                                                           table_info('Serviceescalation_contacts','contact_object_id'),
                                                           table_info('Serviceescalations','service_object_id'),
                                                           table_info('Servicegroup_members','service_object_id'),
                                                           table_info('Servicegroups','servicegroup_object_id'),
                                                           table_info('Services','service_object_id'),
                                                           table_info('Timeperiods','timeperiod_object_id')                                                        
                                                           );

但是我有以下编译错误:

PLS-00222: no function with name 'TABLE_INFO' exists in this scope.

此类型在包描述中声明,config_tables集合的初始化在包体中完成。

由于

2 个答案:

答案 0 :(得分:2)

以下“黑客”应该可以解决问题!

declare

type table_info IS RECORD (
table_name VARCHAR2(50),
join_column VARCHAR2(50)
);

type config_tables_type is table of table_info;

config_tables config_tables_type;

function table_info_constructor(table_name VARCHAR2, join_column VARCHAR2) return table_info 
is
    t_i table_info;
  begin
    t_i.table_name := table_name;
    t_i.join_column := join_column;

    return(t_i);
end;


begin
config_tables := config_tables_type(table_info_constructor('Commands','object_id'),
                                                           table_info_constructor('Contact_notificationcommands','command_object_id'),
                                                           table_info_constructor('Contactgroup_members','contact_object_id'),
                                                           table_info_constructor('Contactgroups','contact_object_id'),
                                                           table_info_constructor('Contactnotificationmethods','command_object_id'),
                                                           table_info_constructor('customvariables','object_id'),
                                                           table_info_constructor('Host_contactgroups','host_id'),
                                                           table_info_constructor('Host_contacts','host_id'),
                                                           table_info_constructor('Hostescalation_contactgroups','contactgroup_object_id'),
                                                           table_info_constructor('Hostescalation_contacts','contact_object_id'),                                                          
                                                           table_info_constructor('Host_parenthosts','parent_host_object_id'),
                                                           table_info_constructor('Hostdependencies','host_object_id'),
                                                           table_info_constructor('Hostdependencies','dependent_host_object_id'),
                                                           table_info_constructor('Hostescalations','host_object_id'),
                                                           table_info_constructor('Hostgroup_members','host_object_id'),
                                                           table_info_constructor('Hostgroups','hostgroup_object_id'),
                                                           table_info_constructor('Hosts','host_object_id'),
                                                           table_info_constructor('Service_contactgroups','contactgroup_object_id'),
                                                           table_info_constructor('Service_contacts','contact_object_id'),
                                                           table_info_constructor('Servicedependencies','service_object_id'),
                                                           table_info_constructor('Serviceescalation_contactgroups','contactgroup_object_id'),
                                                           table_info_constructor('Serviceescalation_contacts','contact_object_id'),
                                                           table_info_constructor('Serviceescalations','service_object_id'),
                                                           table_info_constructor('Servicegroup_members','service_object_id'),
                                                           table_info_constructor('Servicegroups','servicegroup_object_id'),
                                                           table_info_constructor('Services','service_object_id'),
                                                           table_info_constructor('Timeperiods','timeperiod_object_id')                                                        
                                                           );
end;

让我知道它是如何为你工作的。

如有任何进一步的澄清,请不要犹豫。

泰德。

答案 1 :(得分:1)

还有另一种方法,你可以基本上创建一个模式级对象,即OBJECT类型和TABLE类型,然后在plsql块中调用它,如下所示。希望这也有帮助。

--Create object type 
CREATE OR REPLACE TYPE table_info
IS
  OBJECT
  (
    table_name  VARCHAR2(50),
    join_column VARCHAR2(50))
/

--Create table type on Object type    
CREATE OR REPLACE TYPE config_tables_type
IS
  TABLE OF table_info
  /


--PLSQL block of code
DECLARE
  config_tables config_tables_type;
BEGIN
  config_tables config_tables_type := config_tables_type (table_info('Commands','object_id'),
                                                           table_info('Contact_notificationcommands','command_object_id'),
                                                           table_info('Contactgroup_members','contact_object_id'),
                                                           table_info('Contactgroups','contact_object_id'),
                                                           table_info('Contactnotificationmethods','command_object_id'),
                                                           table_info('customvariables','object_id'),
                                                           table_info('Host_contactgroups','host_id'),
                                                           table_info('Host_contacts','host_id'),
                                                           table_info('Hostescalation_contactgroups','contactgroup_object_id'),
                                                           table_info('Hostescalation_contacts','contact_object_id'),                                                          
                                                           table_info('Host_parenthosts','parent_host_object_id'),
                                                           table_info('Hostdependencies','host_object_id'),
                                                           table_info('Hostdependencies','dependent_host_object_id'),
                                                           table_info('Hostescalations','host_object_id'),
                                                           table_info('Hostgroup_members','host_object_id'),
                                                           table_info('Hostgroups','hostgroup_object_id'),
                                                           table_info('Hosts','host_object_id'),
                                                           table_info('Service_contactgroups','contactgroup_object_id'),
                                                           table_info('Service_contacts','contact_object_id'),
                                                           table_info('Servicedependencies','service_object_id'),
                                                           table_info('Serviceescalation_contactgroups','contactgroup_object_id'),
                                                           table_info('Serviceescalation_contacts','contact_object_id'),
                                                           table_info('Serviceescalations','service_object_id'),
                                                           table_info('Servicegroup_members','service_object_id'),
                                                           table_info('Servicegroups','servicegroup_object_id'),
                                                           table_info('Services','service_object_id'),
                                                           table_info('Timeperiods','timeperiod_object_id')                                                        
                                                           );
END;
/