如何动态地将列值插入table2 select * from另一个table1,其中一列比table2小

时间:2017-07-07 07:44:33

标签: oracle plsql

table1:

t1_column1  t1_column2  t1_column3
 1           2           3
 2           3           4
table2 is : 

t2_column1  t2_column2  t2_column3   t2_column4
 1           2           3             0
 2           3           4             0
  

这里我将值作为函数参数传递,但是当我这样做时   尝试创建此函数获取此错误:错误(11,7):   PL / SQL:忽略SQL语句和错误(11,108):PL / SQL:ORA-00917:   缺少逗号

CREATE OR REPLACE FUNCTION NEW_PURCHASE(GODOWN_CODE IN VARCHAR2,MON IN 
VARCHAR2)
   RETURN VARCHAR2 IS
   var_mon VARCHAR2(100);
   PRAGMA AUTONOMOUS_TRANSACTION;

BEGIN
var_mon:=MON;

  INSERT INTO table2  
( t2_column1, t2_column2, t2_column3, var_mon)
SELECT  t1_column1, t1_column2, t1_column3 
FROM table1   WHERE  t1_column1=GODOWN_CODE;
  DELETE FROM table1 WHERE  t1_column1=GODOWN_CODE;

    COMMIT;        
  RETURN 'done';
 END NEW_PURCHASE;

1 个答案:

答案 0 :(得分:0)

试试这个:

CREATE OR REPLACE FUNCTION NEW_PURCHASE (GODOWN_CODE   IN VARCHAR2,
                                         MON           IN VARCHAR2)
   RETURN VARCHAR2
IS
   --var_mon   VARCHAR2 (100);
   PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   --var_mon := 'MON';

   INSERT INTO table2 (t2_column1,
                       t2_column2,
                       t2_column3,
                       t2_column4)
      SELECT t1_column1, t1_column2, t1_column3, mon           
      FROM table1
       WHERE t1_column1 = GODOWN_CODE;

   DELETE FROM table1
         WHERE t1_column1 = GODOWN_CODE;

   COMMIT;
   RETURN 'done';
END NEW_PURCHASE;

请注意,如果您在DML中执行function操作,则cannotselect语句调用它,您需要一个PLSQL块来运行它。< / p>

例如:

您可以按以下方式调用它:

declare
   l_message varchar2(30);
begin
   l_message := test_func('123');
end;

......但不是这样的:

select test_func(empno) from emp;