如何在oracle存储过程中添加列并用值填充它

时间:2018-06-07 18:39:58

标签: oracle stored-procedures plsql

我的研究任务是创建一个程序,将新的列'BRUTTO'添加到表中,并使用基于已定义变量= 24的值和来自“NETTO”列的值填充其记录。我写了这个,但它不起作用。任何帮助表示赞赏。

CREATE OR REPLACE PROCEDURE WSTAW_KOLUMNE_BRUTTO  
AS  
   CURSOR C IS   
     SELECT NETTO  
     FROM TABELA_1  
     FOR UPDATE
     ;
     V_VAT NUMBER(9,2) := 24;
BEGIN
    EXECUTE IMMEDIATE 'ALTER TABLE TABELA_1 ADD BRUTTO NUMBER';
    FOR V_REKORD IN C
    LOOP
       EXECUTE IMMEDIATE 'UPDATE TABELA_1 SET BRUTTO = ' 
           || V_REKORD.NETTO * (1 + 24/100)||' WHERE CURRENT OF C';
    END LOOP;
END WSTAW_KOLUMNE_BRUTTO;

1 个答案:

答案 0 :(得分:2)

我会把它重写为:

CREATE OR REPLACE PROCEDURE WSTAW_KOLUMNE_BRUTTO  
AS  
    V_VAT NUMBER(9,2) := 24;  -- maybe this should be parameter?
BEGIN
   -- how about adding check if column already exists?
   EXECUTE IMMEDIATE 'ALTER TABLE TABELA_1 ADD BRUTTO NUMBER';

   EXECUTE IMMEDIATE 
       'UPDATE TABELA_1 SET BRUTTO = NETTO * (1 + :1 /100)' USING  V_VAT;
END WSTAW_KOLUMNE_BRUTTO;

<强> DBFiddle Demo