SQL Ñ»·Óï¾ä while ½éÉÜ ÊµÀý ¼ò½é
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
---------------
while Ìõ¼þ
begin
Ö´ÐвÙ×÷
set @i=@i+1
end
WHILE
ÉèÖÃÖØ¸´Ö´ÐÐ SQL Óï¾ä»òÓï¾ä¿éµÄÌõ¼þ¡£Ö»ÒªÖ¸¶¨µÄÌõ¼þÎªÕæ£¬¾ÍÖØ¸´Ö´ÐÐÓï¾ä¡£¿ÉÒÔʹÓà BREAK ºÍ CONTINUE ¹Ø¼ü×ÖÔÚÑ»·ÄÚ²¿¿ØÖÆ WHILE Ñ»·ÖÐÓï¾äµÄÖ´ÐС£
Óï·¨
WHILE Boolean_expression
{ sql_statement | statement_block }
[ BREAK ]
{ sql_statement | statement_block }
[ CONTINUE ]
²ÎÊý
Boolean_expression
·µ»Ø TRUE »ò FALSE µÄ±í´ïʽ¡£Èç¹û²¼¶û±í´ïʽÖк¬ÓÐ SELECT Óï¾ä£¬±ØÐëÓÃÔ²À¨ºÅ½« SELECT Óï¾äÀ¨ÆðÀ´¡£
{sql_statement | statement_block}
Transact-SQL Óï¾ä»òÓÃÓï¾ä¿é¶¨ÒåµÄÓï¾ä·Ö×é¡£ÈôÒª¶¨ÒåÓï¾ä¿é£¬ÇëʹÓÿØÖÆÁ÷¹Ø¼ü×Ö BEGIN ºÍ END¡£
BREAK
µ¼Ö´Ó×îÄÚ²ãµÄ WHILE Ñ»·ÖÐÍ˳ö¡£½«Ö´ÐгöÏÖÔÚ END ¹Ø¼ü×ÖºóÃæµÄÈκÎÓï¾ä£¬END ¹Ø¼ü×ÖΪѻ·½áÊø±ê¼Ç¡£
CONTINUE
ʹ WHILE Ñ»·ÖØÐ¿ªÊ¼Ö´ÐУ¬ºöÂÔ CONTINUE ¹Ø¼ü×ÖºóµÄÈκÎÓï¾ä¡£
×¢ÊÍ
Èç¹ûǶÌ×ÁËÁ½¸ö»ò¶à¸ö WHILE Ñ»·£¬ÄÚ²ãµÄ BREAK ½«µ¼ÖÂÍ˳öµ½ÏÂÒ»¸öÍâ²ãÑ»·¡£Ê×ÏÈÔËÐÐÄÚ²ãÑ»·½áÊøÖ®ºóµÄËùÓÐÓï¾ä£¬È»ºóÏÂÒ»¸öÍâ²ãÑ»·ÖØÐ¿ªÊ¼Ö´ÐС£
ʾÀý
A. ÔÚǶÌ×µÄ IF...ELSE ºÍ WHILE ÖÐʹÓà BREAK ºÍ CONTINUE
ÔÚÏÂÀýÖУ¬Èç¹ûƽ¾ù¼Û¸ñÉÙÓÚ $30£¬WHILE Ñ»·¾Í½«¼Û¸ñ¼Ó±¶£¬È»ºóÑ¡Ôñ×î¸ß¼Û¡£Èç¹û×î¸ß¼ÛÉÙÓÚ»òµÈÓÚ $50£¬WHILE Ñ»·ÖØÐÂÆô¶¯²¢Ôٴν«¼Û¸ñ¼Ó±¶¡£¸ÃÑ»·²»¶ÏµØ½«¼Û¸ñ¼Ó±¶Ö±µ½×î¸ß¼Û¸ñ³¬¹ý $50£¬È»ºóÍ˳ö WHILE Ñ»·²¢´òÓ¡Ò»ÌõÏûÏ¢¡£
USE pubs
GO
WHILE (SELECT AVG(price) from titles) < $30
BEGIN
UPDATE titles
SET price = price * 2
SELECT MAX(price) from titles
IF (SELECT MAX(price) from titles) > $50
BREAK
ELSE
CONTINUE
END
PRINT 'Too much for the market to bear'
B. ÔÚ´øÓÐÓαêµÄ¹ý³ÌÖÐʹÓà WHILE
ÒÔÏ嵀 WHILE ½á¹¹ÊÇÃûΪ count_all_rows
Ïà¹ØÎĵµ£º
ÔÚSQLÖÐSelectÓï¾äµÄÖ´ÐÐ˳ÐòÈçÏ£º
(1)from×Ӿ䣬×é×°À´×Ô²»Í¬Êý¾ÝÔ´µÄÊý¾Ý
(2)WHERE×Ӿ䣬¸øÓèÖ¸¶¨Ìõ¼þ¶Ô¼Ç¼½øÐÐɸѡ
(3)GROUP BY×Ӿ䣬½«Êý¾Ý»®·ÖΪ¶à¸ö×é
(4)ʹÓþۼ¯º¯Êý½øÐмÆËã
(5)ʹÓÃHAVING×Ó¾äɸѡ·Ö×é
(6)¼ÆËãËùÓбí´ïʽ
(7)ʹÓÃORDER BY¶Ô½á¹û½øÐÐÅÅÐò ......
½ñÌìÔÚÐÞ¸ÄÒÔǰ¿ª·¢µÄÒ»¸öÍøÕ¾µÄʱºòÓöµ½Ò»¸öÎÊÌ⣬ÒòΪ¹¦ÄܵÄÐèÒª£¬ÒÔǰµÄÒ»¸öÊý×ÖÀàÐÍ×ֶθıäΪÎı¾×ֶΣ¬ÒòΪÕâ¸ö×Ö¶ÎÐèÒª°üº¬¶à¸öÀà±ðµÄID£¬ÒÔǰÕâ¸ö×Ö¶ÎÖ»¶ÔÓ¦Ò»¸öIDµÄ¡£
ÍøÕ¾ÊÇASP VBÓïÑԵģ¬Êý¾Ý¿âʹÓõÄÊÇAccess
ÐÞ¸ÄÊý¾ÝÀàÐÍÖ®ºóǰ̨µÄ²éѯ´úÂë¾Í²»ÄÜʹÓÃÖîÈçxID=1ÕâÖÖÌõ¼þÁË¡£ÓÉÓÚÏÖÔÚ¸Ã×ֶαäΪÎı¾ÀàÐ ......
ʹÓÃÕûÊýÊý¾ÝµÄ¾«È·Êý×ÖÊý¾ÝÀàÐÍ¡£
bigint ´Ó -2^63 (-9223372036854775808) µ½ 2^63-1 (9223372036854775807) µÄÕûÐÍÊý¾Ý£¨ËùÓÐÊý×Ö£©¡£
´æ´¢´óСΪ 8 ¸ö×Ö½Ú¡£
int ´Ó -2^31 (-2,147,483,648) µ½ 2^31 - 1 (2,147,483,64 ......
sql server2000ÖÐʹÓÃconvertÀ´È¡µÃdatetimeÊý¾ÝÀàÐÍÑùʽ£¨È«£©
ÈÕÆÚÊý¾Ý¸ñʽµÄ´¦Àí£¬Á½¸öʾÀý£º
CONVERT(varchar(16), ʱ¼äÒ», 20) ½á¹û£º2007-02-01 08:02/*ʱ¼äÒ»°ãΪgetdate()º¯Êý»òÊý¾Ý±íÀïµÄ×Ö¶Î*/
CONVERT(varchar(10), ʱ¼äÒ», 23) ½á¹û£º2007-02-01 /*varchar(10)±íʾÈÕÆÚÊä³öµÄ¸ñʽ£¬Èç¹û²»¹»³¤»á·¢Éú½ØÈ¡*/ ......
MySQL·þÎñÆ÷°üº¬Ò»Ð©ÆäËûSQL DBMSÖв»¾ß±¸µÄÀ©Õ¹¡£×¢Ò⣬Èç¹ûʹÓÃÁËËüÃÇ£¬½«ÎÞ·¨°Ñ´úÂëÒÆÖ²µ½ÆäËûSQL·þÎñÆ÷¡£ÔÚijЩÇé¿öÏ£¬Äã¿ÉÒÔ±àд°üº¬MySQLÀ©Õ¹µÄ´úÂ룬µ«ÈÔ±£³ÖÆä¿ÉÒÆÖ²ÐÔ£¬·½·¨ÊÇÓÓ/*... */”×¢Ê͵ôÕâЩÀ©Õ¹¡£MySQL·þÎñÆ÷Äܹ»½âÎö²¢Ö´ÐÐ×¢ÊÍÖеĴúÂ룬¾ÍÏñ¶Ô´ýÆäËûMySQLÓï¾äÒ»Ñù£¬µ«ÆäËûSQL·þÎñÆ÷½«ºöÂÔ ......