sqlÖÐÑ»·Óï¾äÔõôд
sql Ñ»·Óï¾äÔÊÐíÖظ´Ö´ÐÐÓï¾ä¿é£¬Ö±µ½Öª×ãÌõ¼þ£¬¹²Ö§³ÖËÄÖÖÑ»·£ºwhile Ñ»·£ºÖ»ÒªÌõ¼þΪÕæ¼´Öظ´Ö´ÐУ»repeat Ñ»·£ºÓï¾ä¿éÖÁÉÙÖ´ÐÐÒ»´Î£¬Ö±ÖÁÌõ¼þΪÕ棻for Ñ»·£ºÆ¾Ö¤Ö¸¶¨²½³¤µÝÔöÑ»·±äÁ¿£¬²¢ÔÚÿ´Îµü´úʱִÐÐÓï¾ä¿é£»ÓαêÑ»·£ºÍ¨¹ýÓαê´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý£¬²¢Ñ»·±éÀúЧ¹û¼¯¡£
SQL ÖÐÑ»·Óï¾ä
Ñ»·Óï¾äÔÊÐíÄúÖظ´Ö´ÐпéÖеÄÒ»×éÓï¾ä£¬Ö±µ½Öª×ãÌض¨Ìõ¼þ¡£SQL ÖÐÖ§³ÖÒÔÏÂÑ»·Óï¾ä£º
WHILE Ñ»·
Óï·¨£º
WHILE Ìõ¼þ DO Óï¾ä¿é END WHILE;
µÇ¼ºó¸´ÖÆ
˵Ã÷£ºÖ»ÒªÌõ¼þΪÕ棬¾Í»áÖظ´Ö´ÐÐÓï¾ä¿é¡£
REPEAT Ñ»·
Óï·¨£º
REPEAT Óï¾ä¿é UNTIL Ìõ¼þ;
µÇ¼ºó¸´ÖÆ
˵Ã÷£ºÓï¾ä¿éÖÁÉÙÖ´ÐÐÒ»´Î£¬Ö±µ½Ìõ¼þΪÕæΪֹ¡£
FOR Ñ»·
Óï·¨£º
FOR iterator_variable IN (start_value, end_value [, increment]) DO Óï¾ä¿é END FOR;
µÇ¼ºó¸´ÖÆ
˵Ã÷£º½«Ñ»·±äÁ¿´Ó start_value µÝÔöµ½ end_value£¬²¢ÔÚÿ´Îµü´úʱִÐÐÓï¾ä¿é¡£increment ÊÇ¿ÉÑ¡µÄ£¬Ä¬ÒÔΪ 1¡£
ÓαêÑ»·
Óï·¨£º
DECLARE cursor_name CURSOR FOR query; OPEN cursor_name; FETCH cursor_name INTO variable_list; WHILE @@FETCH_STATUS = 0 BEGIN Óï¾ä¿é FETCH cursor_name INTO variable_list; END CLOSE cursor_name; DEALLOCATE cursor_name;
µÇ¼ºó¸´ÖÆ
˵Ã÷£º½¨ÉèÓα겢´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý£¬È»ºóÑ»·±éÀúЧ¹û¼¯²¢Ö´ÐÐÓï¾ä¿é¡£
ʾÀý
ʹÓà WHILE Ñ»·ÇóºÍ£º
DECLARE @sum INT = 0; DECLARE @i INT = 1; WHILE @i <p><strong>ʹÓà FOR Ñ»·±éÀú±í£º</strong></p><pre class="brush:php;toolbar:false">DECLARE @table_name TABLE (id INT, name VARCHAR(50)); FOR row IN @table_name DO PRINT row.id + ' ' + row.name; END;
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖÐÑ»·Óï¾äÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡