Mysql預存程序遊標迴圈操作

來源:互聯網
上載者:User
代碼

LABEL_PROC:
BEGIN
    
    -- 任務表關聯時使用的key值
    DECLARE v_faxkey VARCHAR(50) default '';

    -- 任務表中的傳真狀態
    DECLARE v_taskstate int default 0;
    DECLARE v_ID int default 0;
    DECLARE v_userName VARCHAR(50) default '';

    DECLARE done INT DEFAULT 0;
    DECLARE cur1 CURSOR FOR SELECT ID FROM childfax where ParentId=p_id;
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

    -- 如果id不在合法範圍,直接返回-1
    if( p_id<=0 ) then
        select -1 into result;
        leave LABEL_PROC; 
    end if;

    -- id 指定記錄不存在,直接返回 -2
    if(not exists(select * from parentfax where id=p_id) ) then
        select -2 into result;
        leave LABEL_PROC;
    end if;

  if(select count(*) from faxtask where ParentId = p_id and status=1 > 0) then
        select -3 into result;
        select '正在發送中,不能刪除';
                leave LABEL_PROC;
  end if;
  

    -- 設定子傳真會刪除狀態 
    UPDATE childfax set faxstate=-1 where ParentId=p_id;
        
    -- 設定份傳真為刪除狀態
    UPDATE parentfax set FaxState=-1 where id=p_id;
  
  -- 刪除任務表中資料
  delete from faxtask where parentid = p_id;

  OPEN cur1;
    
    REPEAT
            FETCH cur1 INTO v_ID;
        
            IF NOT done THEN
                    select username into v_userName from user_childfax_view where id=v_ID;
                    call pro_add_log(v_ID,2,concat(v_userName,' 刪除') );
            END IF;

        UNTIL done END REPEAT;
      CLOSE cur1;
  
    -- 返回執行成功結果
    select 1 into result;

END LABEL_PROC

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.