簡單遊標使用

需求:sql

比較post_info和post_info_if表。若是post_info_if中某條數據在post_info中則更新,若是沒有則插入,而且將異常的某條記錄打上標誌。該存儲過程比較靈活,能夠在if中能夠繼續添加條件及處理方式。post

createorreplaceprocedure fn_merge_post is
begin
  DECLARE
        CURSOR c_dl IS    //定義一個遊標
            (select id , post_name,    dep_id,  department_id
        from post_info_if
       );
        c_row c_dl%ROWTYPE;  //聲明一個遊標變量,及類型
        numb number;  //聲明變量及類型
        v_sqlfalg varchar(400);//聲明變量及類型
   BEGIN
        FOR c_row IN c_dl LOOP  //循環遍歷遊標,將遊標中的一條數據放到c_row中
          begin
               v_sqlfalg   := 'SELECT count(*) FROM  post_info  WHERE  post_id=''' ||c_row.ID ||'''';  
               execute immediate v_sqlfalg into numb;      //變量賦值   
           if numb>0 then  //判斷是更新仍是插入操做
                          update post_info oo set
                           oo.post_name = c_row.post_name,
                           oo.dep_id = c_row.dep_id,
                           oo.department_id = c_row.DEPARTMENT_ID;
                           COMMIT;
           elsif numb=0 then
                          insert into post_info(POST_ID,post_name,dep_id,DEPARTMENT_ID)  values(c_row.id,c_row.post_name,c_row.dep_id,c_row.department_id);
               COMMIT;
           END IF;
           EXCEPTION   //異常處理
                WHEN OTHERS THEN
                    ROLLBACK;   //發生異常,並回滾。
                    BEGIN   
                       dbms_output.put_line('出錯ID:'||c_row.id);  //打印錯誤數據id
                       update post_info_if set sychro_flag ='-1' where id = c_row.id;  //把錯誤數據打上標記位
                         COMMIT;
                    END;
          end;
        END LOOP;
    END;
end fn_merge_post;

需求更新:當該條數據的結束時間小於當前時間,該條數據要刪除,不然更新或插入該條數據

create or replace procedure fn_merge_post is
begin
  DECLARE
        CURSOR c_dl IS
            (select id , post_name,    dep_id,  department_id ,end_date
        from post_info_if
       );
        c_row c_dl%ROWTYPE;
        numb number;
        v_sqlfalg varchar(400);
        currentDate date;
        v_err_msg varchar2(1000);
        i_code  number:=0;
   BEGIN
        select sysdate into currentDate from dual;
        FOR c_row IN c_dl LOOP
          begin             
               v_sqlfalg   := 'SELECT count(*) FROM  post_info  WHERE  post_id=''' ||c_row.ID ||'''';
               execute immediate v_sqlfalg into numb;                
           if numb>0 then
                          if c_row.end_date <= currentDate then
                             dbms_output.put_line('刪除時間:'||c_row.end_date);
                             delete from post_info where post_id=c_row.id;
                          elsif  c_row.end_date > currentDate then
                            update post_info oo set
                             oo.post_name = c_row.post_name,
                             oo.dep_id = c_row.dep_id,
                             oo.department_id = c_row.DEPARTMENT_ID;
                             COMMIT;
                           end if;
           elsif numb=0 then
                          insert into post_info(POST_ID,post_name,dep_id,DEPARTMENT_ID)  values(c_row.id,c_row.post_name,c_row.dep_id,c_row.department_id);
               COMMIT;
           END IF;
           EXCEPTION
                WHEN OTHERS THEN
                    ROLLBACK;           
                    BEGIN
                       v_err_msg:=sqlerrm;
                       i_code:=to_char(sqlcode);
                       dbms_output.put_line('錯誤信息:'||v_err_msg);
                       update post_info_if set sychro_flag ='-1',erro_msg= v_err_msg where id = c_row.id;
                         COMMIT;
                    END;    
          end;         
        END LOOP;
    END;
end fn_merge_post;
相關文章
相關標籤/搜索