SQLÊý¾Ý¿â
CURSOR
==================================
l SQL ÓαêCURSORµÄʹÓÃ
ʹÓÃÆðÀ´ºÜ¼òµ¥£¬Ïȶ¨Ò壬Ȼºó¸³¸öÖµ£¬´ò¿ª£¬Í¨¹ýWhile Loop Ò»¸öÒ»¸ö¶ÁÏÂÈ¥£¬×îºó¹Ø±Õ£¬ÊÍ·ÅÄÚ´æ¡£»ù±¾Ì×·ÈçÏ£º
DECLARE MyCursor cursor /* ÉùÃ÷Óα꣬ĬÈÏΪµ¥´¿ÏòÇ°µÄÓαꡣÈç¹ûÏëҪǰºóÌøÀ´ÌøÈ¥µÄ£¬Ð´³ÉScroll Cursor¼´¿É */
FOR
SELECT Column1, Column2 from ijij±í
Where ijijÌõ¼þ
OPEN MyCursor /* ´ò¿ªÓαê */
FETCH NEXT from MyCursor Into @A, @B /* ¶ÁÈ¡µÚ1ÐÐÊý¾Ý*/
WHILE @@FETCH_STATUS = 0 /* ÓÃWHILEÑ»·¿ØÖÆÓαê */
BEGIN /*BEGIN-END ¿é*/
//ÔÚÕâÀï,ÓÃ@A,@B×öµãÊÂ,»òÕ߸ÄÒ»¸ÄֵʲôµÄ.
FETCH NEXT from MyCursor Into @A, @B /* ×¥ÏÂÒ»ÐÐ */
END
CLOSE MyCursor /* ¹Ø±ÕÓαê */
DEALLOCATE MyCursor /* ɾ³ýÓαê,ÊÍ·ÅÄÚ´æ */
ÉùÃ÷Óαê///////////////////////
USE pubs
GO
DECLARE authors_cursor CURSOR FOR
SELECT au_id,au_fname,au_lname
from authors
GO
USE pubs
GO
DECLARE authors_cursor1 CURSOR FOR
SELECT au_id,au_fname,au_lname
from authors
FOR READ ONLY
GO
USE pubs
GO
DECLARE authors_cursor2 CURSOR FOR
SELECT au_id,au_fname,au_lname
from authors
FOR UPDATE
GO
´ò¿ªÓαê////////////
DECLARE mancursor CURSOR FOR
SELECT *
from db_manpowerinfo.dbo.ÈËʱí
WHERE 񅧏='00019'
OPEN mancursor
FETCH NEXT from mancursor
CLOSE mancursor
¶ÁÈ¡ÓαêÖеÄÊý¾Ý/////////////////////
DECLARE mancursor1 CURSOR FOR
SELECT ±àºÅ,ÐÕÃû,ÐÔ±ð,Éí·ÝÖ¤ºÅ
from db_manpowerinfo.dbo.ÈËʱí
OPEN mancursor1
FETCH NEXT from mancursor1
WHILE @@FETCH_STATUS=0
BEGIN
FETCH NEXT from mancursor1
END
CLOSE mancursor1
DEALLOCATE mancursor1
¹Ø±Õ²¢ÊÍ·ÅÓαê//////////////////
USE ÏúÊÛ¹ÜÀíϵͳ
GO
DECLARE mycursor CURSOR FOR
SELECT ¿Í»§È«³Æ,count(*) as ÏúÊۼǼÊý,sum(ÊýÁ¿)as ÏúÊÛÊýÁ¿ from ÏúÊÛ±í WHERE ÊýÁ¿ >100 Group BY ¿Í»§È«³Æ
OPEN mycursor
FETCH NEXT from mycursor
WHILE @@FETCH_STATUS = 0 -- ÅжÏÊÇ·ñ»¹ÓÐÐëÒª¶ÁÈ¡µÄ¼Ç¼
BEGIN
FETCH NEXT from mycursor --¶ÁÈ¡¼Ç¼µ½Óαê
END
CLOSE mycursor
DEALLOCATE mycurso
Ïà¹ØÎĵµ£º
ÔÚÊý¾Ý¿âÓ¦ÓõÄÉè¼ÆÖУ¬ÎÒÃÇÍùÍù»áÐèÒª»ñȡijЩ±íµÄ¼Ç¼×ÜÊý£¬ÓÃÓÚÅжϱíµÄ¼Ç¼×ÜÊýÊÇ·ñ¹ý´ó£¬ÊÇ·ñÐèÒª±¸·ÝÊý¾ÝµÈ¡£ÎÒÃÇͨ³£µÄ×ö·¨ÊÇ£ºselect count(*) as c from tableA ¡£È»¶ø¶ÔÓڼǼÊý¾Þ´óµÄ±í£¬ÉÏÊö×ö·¨½«»á·Ç³£ºÄʱ¡£ÔÚDELL 4400 ·þÎñÆ÷ÉÏ×öÊÔÑ飬MS Sqlserver 2000 Êý¾Ý¿â¶ÔÓÚ100Íò¼Ç¼µÄ¼òµ¥Êý¾Ý±íÖ´ÐÐÉÏÊöÓï¾ä£¬Ê± ......
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃExecÖ´ÐÐ
Àý: Select * from tableName
Exec('select * from tableName')
& ......
CHECK Ô¼Êø(CHECK Ô¼Êø:¶¨ÒåÁÐÖпɽÓÊܵÄÊý¾ÝÖµ¡£¿ÉÒÔ½« CHECK Ô¼ÊøÓ¦ÓÃÓÚ¶à¸öÁУ¬Ò²¿ÉÒÔ½«¶à¸ö CHECK Ô¼ÊøÓ¦ÓÃÓÚµ¥¸öÁС£µ±³ýȥij¸ö±íʱ£¬Ò²½«³ýÈ¥ CHECK Ô¼Êø¡£)Ö¸¶¨¿ÉÓɱíÖÐÒ»Áлò¶àÁнÓÊܵÄÊý¾ÝÖµ»ò¸ñʽ¡£ÀýÈ磬¿ÉÒÔÒªÇó authors ±íµÄ zip ÁÐÖ»ÔÊÐíÊäÈëÎåλÊýµÄÊý×ÖÏî¡£
¡¡¡¡
¡¡¡¡¿ÉÒÔΪһ¸ö±í¶¨ÒåÐí¶à CHECK Ô¼Êø¡£¿ ......
ÔÚѧϰSQLʱ¿´µ½µÄһƬºÜºÃµÄÎÄÕ£¬ÌØÌù³öÀ´ºÍ´ó¼ÒÒ»Æð·ÖÏí£¡
ÎÒÃÇÒª×öµ½²»µ«»áдSQL,»¹Òª×öµ½Ð´³öÐÔÄÜÓÅÁ¼µÄSQLÓï¾ä¡£
£¨1£©Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)£º
OracleµÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬from×Ó¾äÖÐдÔÚ×îºóµÄ±í(»ù´¡±í driving table)½«±»×îÏÈ´¦ ......