½²½âMSSQLÊý¾Ý¿âÖÐSQLËø»úÖƺÍÊÂÎñ¸ôÀ뼶±ð
Ëø»úÖÆ
NOLOCKºÍREADPASTµÄÇø±ð¡£
1. ¿ªÆôÒ»¸öÊÂÎñÖ´ÐвåÈëÊý¾ÝµÄ²Ù×÷¡£
BEGIN TRAN t
INSERT INTO Customer
SELECT 'a','a'
2. Ö´ÐÐÒ»Ìõ²éѯÓï¾ä¡£
SELECT * from Customer WITH (NOLOCK)
½á¹ûÖÐÏÔʾ"a"ºÍ"a"¡£µ±1ÖÐÊÂÎñ»Ø¹öºó£¬ÄÇôa½«³ÉΪÔàÊý¾Ý¡£(×¢:1ÖеÄÊÂÎñδÌá½») ¡£NOLOCK±íÃ÷ûÓжÔÊý¾Ý±íÌí¼Ó¹²ÏíËøÒÔ×èÖ¹ÆäËüÊÂÎñ¶ÔÊý¾Ý±íÊý¾ÝµÄÐ޸ġ£
SELECT * from Customer
ÕâÌõÓï¾ä½«Ò»Ö±ËÀËø£¬Ö±µ½ÅÅËûËø½â³ý»òÕßËø³¬Ê±ÎªÖ¹¡£(×¢:ÉèÖÃËø³¬Ê±SET LOCK_TIMEOUT 1800)
SELECT * from Customer WITH (READPAST)
ÕâÌõÓï¾ä½«ÏÔʾaδÌύǰµÄ״̬£¬µ«²»Ëø¶¨Õû¸ö±í¡£Õâ¸öÌáʾָÃ÷Êý¾Ý¿âÒýÇæ·µ»Ø½á¹ûʱºöÂÔ¼ÓËøµÄÐлòÊý¾ÝÒ³¡£
3. Ö´ÐÐÒ»Ìõ²åÈëÓï¾ä¡£
BEGIN TRAN t
INSERT INTO Customer
SELECT 'b','b'
COMMIT TRAN t
Õâ¸öʱºò£¬¼´Ê¹²½Öè1µÄÊÂÎñ»Ø¹ö£¬ÄÇôaÕâÌõÊý¾Ý½«¶ªÊ§£¬¶øb¼ÌÐø²åÈëÊý¾Ý¿âÖС£
NOLOCK
1£® Ö´ÐÐÈçÏÂÓï¾ä¡£
BEGIN TRAN ttt
SELECT * from Customer WITH (NOLOCK)
WAITFOR delay '00:00:20'
COMMIT TRAN ttt
×¢£ºNOLOCK²»¼ÓÈκÎËø£¬¿ÉÒÔÔöɾ²é¸Ä¶ø²»Ëø¶¨¡£
INSERT INTO Customer SELECT 'a','b' –²»Ëø¶¨
DELETE Customer where ID=1 –²»Ëø¶¨
SELECT * from Customer –²»Ëø¶¨
UPDATE Customer SET Title='aa' WHERE ID=1 –²»Ëø¶¨
ROWLOCK
1. Ö´ÐÐÒ»Ìõ´øÐÐËøµÄ²éѯÓï¾ä¡£
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ -- (±ØÐë)
BEGIN TRAN ttt
SELECT * from Customer WITH (ROWLOCK) WHERE ID=17
WAITFOR delay '00:00:20'
COMMIT TRAN ttt
×¢£ºÔÚɾ³ýºÍ¸üÐÂÕýÔÚ²éѯµÄÊý¾Ýʱ£¬»áËø¶¨Êý¾Ý¡£¶ÔÆäËûδ²éѯµÄÐкÍÔö¼Ó£¬²éѯÊý¾ÝÎÞÓ°Ïì¡£
INSERT INTO Customer SELECT 'a','b' –²»µÈ´ý
DELETE Customer where ID=17 –µÈ´ý
DELETE Customer where ID<>17 –²»µÈ´ý
SELECT * from Customer –²»µÈ´ý
UPDATE Customer SET Title='aa' WHERE ID=17–µÈ´ý
UPDATE Customer SET Title='aa' WHERE ID<>17–²»µÈ´ý
HOLDLOCK£¬TABLOCKºÍTABLOCKX
1. Ö´ÐÐHOLDLOCK
BEGIN TRAN ttt
SELECT * from Customer WITH (HOLDLOCK)
WAITFOR delay '00:00:10'
COMMIT TRAN ttt
×¢£ºÆäËûÊÂÎñ¿ÉÒÔ¶ÁÈ¡±í£¬µ«²»ÄܸüÐÂɾ³ý
update Customer set Title='aa' —ÒªµÈ´ý10ÃëÖС£
SEL
Ïà¹ØÎĵµ£º
Êý¾Ý¿âʵÀý½á¹¹ ÊÂÎñÒ»ÖÂÐÔ(Transactional Consistency)ºÍ»ùÓÚʱ¼äµãµÄ»Ö¸´(Point-in-time Recovery) ϵͳԪÊý¾Ý
topÊý¾Ý¿âʵÀý½á¹¹
µ±OracleʵÀýÆô¶¯Ö®ºó£¬Ëù¿´µ½µÄ¾ÍÊÇÔÚ·þÎñÆ÷ÄÚ´æÉϵÄÒ»¸ö¸ö²»Í¬ÄÚ´æ¿é¼ÓÉϲúÉúµÄÓëÕâЩÄÚ´æ½»»¥µÄºǫ́½ø³Ì¡£OracleÎĵµ½«ÕâЩÄÚ´æ½á¹¹ºÍ½ø³ÌÊյĺÜÏêϸ¡£
ÓÉOracleʵÀýËùÕ¼ÓõÄÄÚ´æ¿é³ ......
Transact-SQL ÓïÑÔʹÓõÄÁ÷³Ì¿ØÖÆÃüÁîÓë³£¼ûµÄ³ÌÐòÉè¼ÆÓïÑÔÀàËÆÖ÷ÒªÓÐÒÔϼ¸ÖÖ¿ØÖÆÃüÁî¡£
4.6.1 IF…ELSE
Æä
Óï·¨ÈçÏ£º
IF <Ìõ¼þ±í´ïʽ>
<ÃüÁîÐлò³ÌÐò¿é>
[ELSE [Ìõ¼þ±í´ïʽ]
<ÃüÁîÐÐ
»ò³ÌÐò¿é>]
ÆäÖÐ<Ìõ¼þ±í´ïʽ>¿ÉÒÔÊǸ÷ÖÖ±í´ïʽµÄ×éºÏ£¬µ«±í´ïʽµÄÖµ±ØÐëÊÇÂß¼Öµ“Õæ&rdq ......
¡¡¡¡1. SQL ServerµÄϵͳ±í
¡¡¡¡MicrosoftµÄSQL ServerÊÇÒ»¸ö¿ÉÉìËõµÄ¸ßÐÔÄÜÊý¾Ý¿â¹ÜÀíϵͳ£¬×¨Îª·Ö²¼Ê½¿Í»§»ú/·þ
ÎñÆ÷»·¾³¶øÉè¼Æ£¬SQL Server¼¸ºõ½«ËùÓеÄÅäÖÃÐÅÏ¢¡¢°²È«ÐÔÐÅÏ¢ºÍ¶ÔÏóÐÅÏ¢¶¼´æ´¢ÔÚÁËËü×Ô
ÉíµÄϵͳ±íÖУ¬¶øϵͳ±í´æÔÚÓÚÿ¸ö¶ÀÁ¢µÄÊý¾Ý¿âÖУ¬´æ´¢Ò»¸öÌض¨Êý¾Ý¿â¶ÔÏóÐÅÏ¢µÄϵͳ±í
ͨ³£³ÆΪÊý¾Ý¿âĿ¼£¬M ......
Ë®»·¾³¼à²âÊý¾Ý´æ´¢½á¹¹ÖÐÓÐÒ»ÖÖģʽ½Ð×ö"Êú±íģʽ"£¬¼´ÔÚ¼à²âÊý¾Ý±íÖУ¬Ä³¸öµãλÔÚij¸öʱ¼äµãÉϸ÷¼à²âÏîÄ¿µÄŨ¶È²â¶¨ÖµÔÚÎïÀí±íÖд洢ÔÚ¶àÌõ¼Ç¼ÖУ¬³ÊÊú×´·Ö²¼¡£¼à²âÊý¾Ý±íÖаüº¬²âµã´úÂë¡¢¼à²âʱ¼ä¡¢¼à²âÏîÄ¿´úÂ롢Ũ¶È²â¶¨ÖµµÈ£¬ËùÓмà²âÏîÄ¿µÄŨ¶È²â¶¨Öµ¶¼´æ´¢ÔÚͬһ¸ö×Ö¶ÎÖУ¬¼ÓÒÔ¼à²âÏîÄ¿´úÂë×÷ΪÇø·Ö×ֶΡ£
"Êú ......
Ò» ϵͳԤ¶¨Òå´íÎó´úÂë
SQL Server ÓÐ3831¸öÔ¤¶¨Òå´íÎó´úÂ룬ÓÉmaster.dbo.sysmessages±íά»¤¡£Ã¿Ò»¸ö´íÎó´úÂ붼ÓÐÏàÓ¦µÄ¼¶±ðºÍÃèÊö¡£
´íÎó¶¨ÒåµÄ¼¶±ð´Ó0µ½25¡£20ÒÔÉϵĴíÎó´ú±íÖØ´ó´íÎó£¬Í¨³£ÒâζןôíÎó»áµ¼Ö´洢½ø³ÌÁ¢¿ÌÖÕÖ¹£¬²¢ÇÒËùÓеĿͻ§Á¬½Ó¶¼ÒªÖØгõʼ»¯¡£
·Ç¹Ø¼üÐÔ´íÎóÖ»ÊǽûÖ¹µôµ±Ç°ÔËÐеijÌÐòÐУ¬²¢¼Ì ......