2012-02-09 108 views
0
DELIMITER // 
DROP PROCEDURE IF EXISTS sp_vk_suspend// 
CREATE PROCEDURE sp_vk_suspend2() 
declare today = DATETIME DEFAULT NULL; 
declare accid = INT DEFAULT 11; 
set today = now(); 
BEGIN  
    SELECT * FROM members where expirydate < today;  
END // 

這是我的存儲過程。我必須從members表中添加一個或多個依賴id的查詢。我如何從成員表中檢索id。MySQL存儲過程檢索值

恩:我來補充該查詢在我的SP

UPDATE members 
SET suspend = 1 
WHERE id = 'some id from mem: tables' 
+0

你需要更新'expirydate triclosan 2012-02-09 08:52:08

+0

是的,我需要更新所有到期的成員。 – pico 2012-02-09 09:00:41

+0

爲什麼不以這種方式嘗試'UPDATE members SET suspend = 1 WHERE expirydate triclosan 2012-02-09 09:03:54

回答

0

我看到恕我直言光標這是你所需要的

DECLARE done INT DEFAULT FALSE; 
DECLARE cur1 CURSOR FOR SELECT id FROM members where expirydate < today; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 
DECLARE a INT; 

OPEN cur1; 

my_loop: LOOP 
    FETCH cur1 INTO a 

    IF done THEN 
     LEAVE my_loop; 
    END IF; 

    UPDATE members SET suspend=1 WHERE id = a; 
    INSERT INTO another(member_id) values(a); 

END LOOP; 
CLOSE cur1;