1

我对 PL/SQL 很陌生,但我想知道是否有办法实现以下目标:

   INSERT  INTO aTable (
   SELECT  FN_TO_VALUE(b.field1, b.field2, b.field3) as alias1,
           FN_TO_VALUE(b.field1, b.field2, b.field3) as alias2,
           FN_TO_VALUE(b.field1, b.field2, b.field3) as alias3,
           c.field1, c.field2
   FROM    bTable b, cTable c
   WHERE   b.alias1= c.field3
   );

鉴于FN_TO_VALUE是一个函数,它返回aTable中的相应数据类型。

我收到以下错误声明:

PL/SQL : SQL Statement ignored (that's located at the INSERT INTO line)
PL/SQL : ORA-00904: "B"."ALIAS1" invalid identifier

勘误表:

  • 我不得不将 where 子句从WHERE b.alias1= c.field1 修改WHERE b.alias1= c.field3因为@Bob Jarvis 提供的解决方案之一不会考虑链接表可能需要的其他字段bTable 和 cTable。

解决方案 :

  • 我选择了以下解决方案:

     INSERT  INTO aTable (
       SELECT b.alias1,
              b.alias2,
              b.alias3,
              c.field1,
              c.field2
       FROM cTable c, 
       (
         SELECT FN_TO_VALUE (field1, field2, field3) AS alias1,
                FN_TO_VALUE (field1, field2, field3) AS alias2,
                FN_TO_VALUE (field1, field2, field3) AS alias3
         FROM bTable 
       ) b
       WHERE b.alias1 = c.field3
    );
    
4

1 回答 1

2
INSERT  INTO aTable (
SELECT inner.alias1,
       inner.alias2,
       inner.alias3,
       inner.field1,
       inner.field2
  FROM (SELECT FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias1,
               FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias2,
               FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias3,
               c.field1,
               c.field2
          FROM bTable b, cTable c
         ) inner
 WHERE inner.alias1 = inner.field1
)

或者

INSERT  INTO aTable (
SELECT FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias1,
       FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias2,
       FN_TO_VALUE (b.field1, b.field2, b.field3) AS alias3,
       c.field1,
       c.field2
  FROM bTable b, cTable c
 WHERE FN_TO_VALUE (b.field1, b.field2, b.field3) = c.field1
   );
于 2013-05-15T10:20:20.520 回答