我可以在oracle函数中使用insert all语句吗?

时间:2012-06-14 16:39:05

标签: oracle function

CREATE OR REPLACE FUNCTION layer2layerAttribute RETURN VARCHAR2 AS
/**
 * This function properly joins all layers to their appropriate fields
 */
  cursor PLACES is select * from layer l where l.layer_name like '%place%';
BEGIN
  FOR place IN PLACES
  LOOP
     Insert all
      into LAYER_JOIN_LAYER_ATTRIBUTE (ID_LAYER, ID_LAYER_ATTRIBUTE) 
        values (place.id_layer, select id_layer_attribute from layer_attribute where name_attribute = 'filter.street')
      into LAYER_JOIN_LAYER_ATTRIBUTE (ID_LAYER, ID_LAYER_ATTRIBUTE) 
        values (place.id_layer, select id_layer_attribute from layer_attribute where name_attribute = 'filter.city')
      into LAYER_JOIN_LAYER_ATTRIBUTE (ID_LAYER, ID_LAYER_ATTRIBUTE) 
        values (place.id_layer, select id_layer_attribute from layer_attribute where name_attribute = 'filter.state')
      into LAYER_JOIN_LAYER_ATTRIBUTE (ID_LAYER, ID_LAYER_ATTRIBUTE) 
        values (place.id_layer, select id_layer_attribute from layer_attribute where name_attribute = 'filter.zip')
      into LAYER_JOIN_LAYER_ATTRIBUTE (ID_LAYER, ID_LAYER_ATTRIBUTE) 
        values (place.id_layer, select id_layer_attribute from layer_attribute where name_attribute = 'filter.country')
      select * from dual;
  END LOOP;
  RETURN null;
END layer2layerAttribute;

1 个答案:

答案 0 :(得分:3)

您可以,但不能在select子句中使用values,也不能在每个values中使用into子句。您可以使用值或单个子查询。 (见the syntax diagram)。我也不确定你为什么要使用游标,而不是选择layer作为子查询,而不是从dual中选择。

您可以使用单个插入执行此操作,同时跳过显式游标;在一个过程中(而不是一个函数,因为你不需要返回任何东西,并且在数据可以更新时通常不使用函数),如果需要的话:

CREATE OR REPLACE PROCEDURE layer2layerAttribute AS
BEGIN
    insert into layer_join_layer_attribute (id_layer, id_layer_attribute)
    select l.id_layer, la.id_layer_attribute
    from layer l
    cross join layer_attribute la
    where l.layer_name like '%place%'
    and la.name_attribute in ('filter.street', 'filter.city', 'filter.state',
        'filter.zip', 'filter.country');
END layer2layerAttribute;

虽然这看起来像填充新表的一次性任务,但您可以将其作为简单的SQL语句运行。如果它不是一次性的,那么除非你检查并排除它们,否则你将在第二次执行时获得重复。