ÎÊÌ⣺ÓαêÑ»·ÖеÄselect£¡ DECLARE v_a int; DECLARE v_b int; DECLARE i int default 0; DECLARE done INT DEFAULT 0; DECLARE cur1 CURSOR FOR select col_a from table_a; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; OPEN cur1; emp_loop: LOOP FETCH cur1 INTO v_a; IF done=1 THEN LEAVE emp_loop; ELSE set i=i+1; select i; select col_b into v_b from table_b where col_b=v_a; END IF; END LOOP emp_loop;
Èç¹ûûÓÐselect col_b from table_b where col_b=v_a;ÓαêÕý³£Ñ»·£¡ ÄÜÑ»·table_aµÄÁÐÊý´Î£¡ µ«ÊÇÓÐÕâ¾äselect col_b into v_b from table_b where col_b=v_a;¾ÍÑ»·ÁËÒ»´Î£¬¾Ídone=1ÁË£¡ ÓÐÕâ¾äselectÓï¾äÄѵÀÆÆ»µÁËÓα꣬ʹµÃÓαêÂíÉÏÍ˳ö£¿ÔÚÏßµÈŶ£¡ ÎÒ±ØÐëÈ¥²éѯ£¬ÒòΪÕâ¸öÊÇÎÒ±ØÐëºóÃæÊ¹ÓõIJÎÊý£¬ÈçºÎ¸Ä£¿ select count(*) into @ee from table_b where col_b=v_a if @ee>=1 then .... end if SQL code: DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;