sqlÖÐloopµÄÓ÷¨
loopÊÇt-sqlÖÐÒ»ÖÖ¿ØÖƽṹ£¬ÓÃÓÚÖظ´Ö´ÐÐÓï¾ä¿é£¬Ö±ÖÁÖª×ãÌض¨Ìõ¼þ¡£ËüÌṩѻ·¿ØÖÆ¡¢ÎÞаÐÔ¡¢¾«Á·ÐÔ£¬µ«Ò²Ðè×¢ÖØÍ˳öÌõ¼þºÍDZÔÚµÄÎÞÏÞÑ»·¡£
SQL ÖÐ LOOP Ó÷¨
ʲôÊÇ LOOP
LOOP ÊÇ Transact-SQL (T-SQL) ÖеÄÒ»ÖÖ¿ØÖƽṹ£¬ËüÔÊÐíÖظ´Ö´ÐÐÒ»×éÓï¾ä£¬Ö±µ½Öª×ãÌض¨Ìõ¼þΪֹ¡£
Óï·¨
LOOP -- Óï¾ä¿é EXIT WHEN <condition>; END LOOP</condition>
µÇ¼ºó¸´ÖÆ
Ó÷¨
LOOP µÄÊÂÇéÔÀíÈçÏ£º
Ö´ÐÐÓï¾ä¿é¡£
¼ì²é EXIT WHEN Ìõ¼þ¡£
ÈôÊÇÌõ¼þΪÕ棬ÔòÍ˳ö LOOP¡£
ÈôÊÇÌõ¼þΪ¼Ù£¬ÔòÖظ´°ì·¨ 1 ºÍ 2¡£
ʾÀý
ÒÔÏÂʾÀýʹÓà LOOP À´ÅÌËã 1 µ½ 10 µÄ×ܺͣº
DECLARE @sum INT; SET @sum = 0; LOOP SET @sum = @sum + 1; EXIT WHEN @sum > 10; END LOOP; PRINT @sum;
µÇ¼ºó¸´ÖÆ
Óŵã
LOOP ÌṩÒÔÏÂÓŵ㣺
Ñ»·¿ØÖÆ£ºÔÊÐíϸÄå¿ØÖÆÑ»·Ö´ÐС£
ÎÞаÐÔ£ºÖ§³Ö×Ô½ç˵Í˳öÌõ¼þ¡£
¾«Á·ÐÔ£ºÓë WHILE Ñ»·Ïà±È£¬Óï·¨¸ü¾«Á·¡£
ÐèҪעÖصĵã
ʹÓà LOOP ʱÐèҪעÖØÒÔϼ¸µã£º
Í˳öÌõ¼þ£ºEXIT WHEN Ìõ¼þ±ØÐèÊDz¼¶û±í´ïʽ£¬²¢ÇÒ±ØÐè×îÖÕΪÕæÒÔÍ˳öÑ»·¡£
ÎÞÏÞÑ»·£ºÈôÊÇûÓÐ EXIT WHEN Ìõ¼þ£¬Ñ»·½«ÎÞÏÞÖ´ÐС£
Ìæ»»ÒªÁ죺ÔÚijЩÇéÐÎÏ£¬WHILE Ñ»·¿ÉÄܸüÊʺÏÌæ»» LOOP¡£
ÒÔÉϾÍÊÇsqlÖÐloopµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡