2
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;
4

1 回答 1

3

可以,但不能select在子句内使用 a values,或代替valueseach 的子句into。您可以使用值,也可以使用单个子查询。(参见语法图)。我也不确定您为什么使用游标,而不是将 select fromlayer作为子查询而不是 select from 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 语句运行。如果它不是一次性的,那么除非您检查并排除它们,否则您将在第二次执行时得到重复。

于 2012-06-14T17:56:45.580 回答