SQL Server2005 ÊÂÎñ¸ôÀ뼶±ð
ÈκÎÊÂÎñÎÞÂÛÆä¸ôÀ뼶±ðÈçºÎ£¬¶¼¾ßÓÐÔ×ÓÐÔ¡£Ô×ÓÐԺ͸ôÀ뼶±ðÊÇÁ½ÂëÊ¡£
¼Ù¶¨Ö»ÓÐÒ»ÖÖ×ÊÔ´Êý¾Ý±íÉϵēÐГ¿ÉÒÔ±»Ëø¶¨¡£ËøÖ»Óй²ÏíËøºÍÅÅËûËøÁ½ÖÖ¡£
ËøµÄ¼æÈÝÐÔ£º
SX
S 10
X 00
ÊÂÎñµÄ¸ôÀ뼶±ð£º
Read Uncommitted
Êý¾Ý¶Áȡʱ²»ÐèÒªËø¶¨
Read Committed
Êý¾Ý¶ÁȡʱÐèÒª¹²ÏíËø¶¨
Repeatable Read
Êý¾Ý¶ÁȡʱÐèÒªÅÅËûËø
Serializable
ÊÂÎñ±ØÐëµÈͬÓÚ´®ÐÐÖ´ÐÐ
×¢ÒâÎÞÂÛÈκÎʱºò¸üÐÂÐж¼±ØÐëÒªÓÐÅÅËûËø¡£
²åÈëÐв»ÐèÒªËø¡£
ÔÚRead Committed¸ôÀ뼶±ðÏ£¬Êý¾Ý¶ÁÈ¡Íê±ÏºóÁ¢¼´ÊͷŹ²ÏíËø£¬¶øÔÚRepeatable Read¸ôÀ뼶±ðÏ£¬ÊÂÎñ±£³Ö¹²Ïí
ËøÖ±µ½Õû¸öÊÂÎñ½áÊø¡£
ÔÚSQL ServerÖе÷ÕûÊÂÎñ¸ôÀ뼶±ðÊÇÕë¶Ô»á»°µÄ£¬set tran isolation levelºó£¬»á»°ÖкóÀ´¿ªÊ¼µÄÊÂÎñ¶¼Ôڴ˸ô
À뼶±ðÉÏÖ´ÐС£Ò»¸öÊÂÎñÖ»ÄܾßÓÐÒ»¸ö¸ôÀ뼶±ð¡£Í¬Ò»»á»°ÖеÄËùÓÐÊÂÎñ±ØÐë´®ÐÐÖ´ÐС£±ØÐëͨ¹ýbegin tranÓï¾äÀ´¸²¸ÇĬÈÏÊÂÎñ·¶Î§¡£
²âÊÔ£º
/*»·¾³*/
CREATE TABLE [dbo].[TranTest](
[Id] [int] IDENTITY(1,1) NOT NULL,
[count1] [int] NULL,
[count2] [int] NULL
)
insert into TranTest values(1,2)
insert into TranTest values(3,4)
insert into TranTest values(5,6)
insert into TranTest values(7,8)
Ô¼¶¨×ÜÊÇÏÈÖ´ÐÐsession1£¬²¢ÇÒÔÚsession1½áÊøÇ°Ö´ÐÐsession2
1. ÅÅËûËø×ÜÊÇÔÚÊÂÎñ½áÊøʱÊÍ·Å
--session 1
set tran isolation level any
begin tran
update TranTest set count1=count1 + 10 where id=1
waitfor delay '00:00:10'
commit tran
--session 2
select * from TranTest
ÏÖÏó£ºsession2±»×èÈûÖ±µ½session1ÖеĻỰ½áÊø¡£
½âÊÍ£ºsession1Ê×ÏÈÖ´ÐУ¬¸üÐÂid=1µÄÐÐʱ³ÖÓÐÁ˸ÃÐеÄÅÅËûËø£¬¸ÃËøÖ±µ½session1ÖеÄÊÂÎñ½áÊøʱÊÍ·Å£¬Õâ×èÖ¹
ÁËsession2»ñµÃ¹²ÏíËø¡£
2. ¶ÁδÌá½»ÎÞÐèÈκÎËø
--session 1
set tran isolation level any
begin tran
update TranTest set count1=count1 + 10 where id=1
waitfor delay '00:00:10'
commit tran
--session 2
set tran isolation level read uncommitted
begin tran
select * from TranTest
commit tran
ÏÖÏó£ºsession2δ±»×èÈû£¬¶øÊǶÁÈ¡µ½ÁËsession1ÉÐδÌá½»µÄÊý¾Ý¡£
½âÊÍ£º¶ÁδÌá½»ÎÞÐèËø¶¨£¬Òò¶øÈÆ¿ªÁËËø¶¨»úÖÆ¡£
3. ËÀËø
--session 1
set tran isolation level read committed
begin tr
Ïà¹ØÎĵµ£º
order by
ÅÅÐòͨ¹ýorder by×Ó¾äʵÏÖ£¬order byÔÚSELECTÓï¾äµÄ×îºó¡£
Óï·¨£º
order by field1 [ASC|DESC][,field2 [ASC|DESC],..,fieldn [ASC|DESC]]
ASC£ºÉýÐò
DESC£º½µÐò
ĬÈÏΪÉýÐò
¿ÕÖµ×÷ΪÎÞÇî´óÀ´´¦Àí¡£
ÁíÍâ¿ÉÒÔ°´ÕÕ²éѯÁбíÖÐÐòºÅ½øÐÐÅÅÐò¡£
ϵͳÔÚÓû§Ð´³ö²éѯÁбíµÄͬʱ¾Í¸³Óèÿ¸öÁÐÃûÒ»¸ ......
Ò»°ãʹÓÃSQL ServerµÄ¼¯³ÉÈÏ֤ʱ£¬¶¼ÐèÒªµÇ½½øÓò£¬ÏÂÃæ½éÉÜÒ»ÖÖ²»ÐèÒªµÇ½Óò¾Í¿ÉÒÔʹÓÃÓòÓû§µÇ½SQL ServerµÄ·½·¨¡£
SQL Server Management Studio 2005ºÍ2008µÄ¿ì½Ý·½Ê½ÈçÏÂͼ£º
SQL Server 2008 x64: "C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
SQL ......
ÔÎÄת×Ô:http://tech.it168.com/a2009/0218/265/000000265868.shtml
Ë÷ÒýÊÇÒÔ±íÁÐΪ»ù´¡µÄÊý¾Ý¿â¶ÔÏó¡£Ë÷ÒýÖб£´æ×űíÖÐÅÅÐòµÄË÷ÒýÁУ¬²¢ÇҼͼÁËË÷ÒýÁÐÔÚÊý¾Ý¿â±íÖеÄÎïÀí´æ´¢Î»Öã¬ÊµÏÖÁ˱íÖÐÊý¾ÝµÄÂß¼ÅÅÐò¡£Í¨¹ýË÷Òý£¬¿ÉÒÔ¼Ó¿ìÊý¾ÝµÄ²éѯËٶȺͼõÉÙϵͳµÄÏìӦʱ¼ä;¿ÉÒÔʹ±íºÍ±íÖ®¼äµÄÁ¬½ÓËٶȼӿ졣
µ«ÊÇ£¬ ......
ÔÚSQL SERVERÖУ¬ÓÐʱÐèÒªºÏ¶àÁÐÖµµ½ÐеÄÐèÇ󣬳£¼ûµÄ·½·¨ÓУº1.´´½¨×Ô¶¨Ò庯Êý£¬2.ʹÓÃÓα귨½øÐÐ×Ö·û´®ºÏ²¢¡£3.ʹÓÃÁÙʱ±íʵÏÖ×Ö·û´®ºÏ²¢¡£ Êý¾Ý£º DEPTNO EMPS
------ ----------
10 CLARK
10 KING
10 MILLER
20 SMITH
20 ADAMS
20 FORD
20 SCOTT
20 JONES
30 ALLEN
30 BLAKE
30 MARTIN
30 JAMES
30 ......
¿´ÁËһƪ½²×ù£¬Ëµµ½Êý¾Ý²ã·ÖÒ³¼¼Êõ£¬Óõ½ÁË4Öз½Ê½£¬1£©Ê¹ÓÃtop *top 2)ʹÓñí±äÁ¿ 3£©Ê¹ÓÃÁÙʱ±í 4£©Ê¹ÓÃROW_NUMBERº¯Êý¡£
ÆäÖÐ×î¿ìµÄÊǵÚ1 ºÍµÚ4Öз½Ê½£¬½ÓÏÂÀ´ÎÒÃÇÀ´¿´¿´ÕâÁ½ÖÖ·½Ê½£º
ÎÒÃÇʹÓÃsql2005×Ô´øµÄÊý¾Ý¿â AdventureWorks²âÊÔ£¬
1£©
--Use Top*Top
DECLARE @Start datetime,@end datetim ......