用于插入,更新或删除的CASE语句

时间:2018-07-02 07:43:24

标签: sql oracle plsql case

我已经在存储过程中创建了。在该存储过程中,我要插入,更新或删除的case语句。

If v_var='I' then insert into main_tbl
If v_var='D' then delete from main_tbl where  <condition>
If v_var='U' then update main_tbl set <condition>.

为此,我创建了存储过程

create or replace procedure sp_cs as
    v_var txt.col1%type;
    type record is ref cursor return txt%rowtype;  --Staging table
    v_rc record;
    cursor c1 is select * from txt;
begin
    open c1;
    loop 
        fetch c1 into v_rc;
        for i in 1 .. v_rc.count
        loop
         select v_rc(i).col1 into v_var from txt;
            case when v_var ='I' then
                 insert into main_tbl values (v_rc(i).col1, .....);
            when v_var ='D' then
                 delete from main_tbl m where m.col2=v_rc(i).col2;
            when v_var ='U' then
                 update main_tbl m set m.col2=v_rc(i).col2;
            end case;
        end loop;
    end loop;
    close c1;
end;

执行此操作后,数据无法加载到 main_tbl 中。您能帮我解决我的问题吗? 谢谢

4 个答案:

答案 0 :(得分:1)

嗯, OPEN..FETCH..LOOP 已经过时了。您应该仅根据参数使用单个insert / update / delete语句,它将比循环结构更有效。

CREATE OR replace PROCEDURE sp_cs (p_var txt.col1%TYPE) 
IS 
BEGIN 
    CASE p_var 
      WHEN 'I' THEN 
        INSERT INTO main_tbl 
        SELECT * 
        FROM   txt; 
      WHEN 'D' THEN 
        DELETE FROM main_tbl m 
        WHERE  EXISTS (SELECT 1 
                       FROM   txt t 
                       WHERE  m.col2 = t.col2); 
      WHEN 'U' THEN 
        UPDATE main_tbl m 
        SET    m.col2 = (SELECT v_rc.col2 
                         FROM   txt 
                         WHERE  m.somecol = t.somecol); -- the column you want to compare while updating
    END CASE; 
END; 

/ 

虽然从更新查询中不清楚您要实现的目标,但是我假设您在txt中更新col2时在main_tbl中有一个公用列。此外,我已经将您的参数用作过程参数p_var。如果您不想传递它,可以将其用作局部变量。

答案 1 :(得分:0)

您需要按原样声明过程,因此第一行缺少一个关键字:

create or replace PROCEDURE sp_cs

您缺少WHEN个关键字和END CASE个关键字。

 case when v_var = 'I'
      then insert into main_tbl values (v_rc(i).col1, .....);
      when v_var = 'D'
      then delete from main_tbl m where m.col2=v_rc.col2;
      when v_var = 'U'
      then update main_tbl m set m.col2=v_rc.col2;
 end case;

答案 2 :(得分:0)

创建过程存在一些语法错误,请尝试以下操作。 此外,请看Oracle MERGE

MERGE可以在一个语句中执行INSERT,UPDATE和DELETE。

CREATE OR REPLACE PROCEDURE sp_cs
IS
   v_var   txt.col1%TYPE;

   TYPE record IS REF CURSOR
      RETURN txt%ROWTYPE;                                      --Staging table

   v_rc    record;

   CURSOR c1
   IS
      SELECT * FROM txt;
BEGIN
   OPEN c1;

   LOOP
      FETCH c1 INTO v_rc;

      FOR i IN 1 .. v_rc.COUNT
      LOOP
         CASE
            WHEN v_var = 'I'
            THEN
               INSERT INTO main_tbl
                    VALUES (v_rc (i).col1);
            WHEN v_var = 'D'
            THEN
               DELETE FROM main_tbl m
                     WHERE m.col2 = v_rc.col2;
            WHEN v_var = 'U'
            THEN
               UPDATE main_tbl m
                  SET m.col2 = v_rc.col2;
         END CASE;
      END LOOP;
   END LOOP;

   CLOSE c1;
END;

答案 3 :(得分:0)

您需要这里的裁判吗?如果没有,请尝试

create or replace procedure sp_cs 
as
    cursor c1 is select * from txt;
    rc c1%rowtype;
begin
    For c in rc Loop
        case c.col1 
            When 'I' Then
                insert into main_tbl values (c.col1, .....);
            When 'U' Then
                update main_tbl m set m.col2=c.col2;
            When 'D'
                delete from main_tbl m where m.col2=c.col2;
        End Case;
    End Loop;
end;