以下面的遍歷sql
CREATE DEFINER = 'root'@'localhost' PROCEDURE zita_wms.DeliveryOrderUtil_ConfirmByList( OUT confirmed_no_ INT, OUT not_confirmed_no_ INT, IN company_id_ INT, IN order_list_ VARCHAR(20000), -- 12,23,4 IN user_id_ VARCHAR(40) ) BEGIN DECLARE order_id_ INT; DECLARE is_confirmed_ INT; DECLARE get_list CURSOR FOR SELECT d.id FROM delivery_order d, sys_array_tab a WHERE d.id = a.content AND d.logistics_company_id = company_id_; DECLARE CONTINUE HANDLER FOR NOT FOUND SET order_id_ = NULL; SET confirmed_no_ = 0 ; SET not_confirmed_no_ = 0 ; CALL sys_array(order_list_, ','); OPEN get_list; order_list: LOOP FETCH get_list INTO order_id_; IF order_id_ IS NULL THEN LEAVE order_list; END IF; -- CALL DeliveryOrderUtil_Confirm(is_confirmed_ , order_id_, user_id_ ); IF is_confirmed_ = 1 THEN -- 1:確認成功 0:確認失敗 SET confirmed_no_ = confirmed_no_ + 1 ; ELSE SET not_confirmed_no_ = not_confirmed_no_ + 1 ; END IF; COMMIT; -- 業務需求, 確認一個訂單就提交一個 END LOOP; CLOSE get_list; END
對應工具類, 建立臨時表,刪除數據,遍歷數據插入工具
CREATE DEFINER = 'root'@'localhost' PROCEDURE zita_wms.sys_array(IN content_ VARCHAR(20000), IN separate_ VARCHAR(1)) BEGIN SET @i_ := 1; SET @count_ := CHAR_LENGTH(content_) - CHAR_LENGTH(REPLACE(content_, separate_,'')) + 1; SET @tmp_ := ''; -- DROP temporary table if exists sys_array_tab; CREATE TEMPORARY TABLE IF NOT EXISTS sys_array_tab( seq_no INT(11), content VARCHAR(2000) ); DELETE FROM sys_array_tab; WHILE @i_ <= @count_ DO SET @tmp_ := SUBSTRING_INDEX(SUBSTRING_INDEX(content_,separate_,@i_),separate_,-1); INSERT INTO sys_array_tab(seq_no, content) VALUES( @i_, @tmp_ ); SET @i_ := @i_ + 1; END WHILE; END