ʹÓà TRY/CATCH Óï¾ä½â¾ö SQL Server 2005 ËÀËø
¶ÔÓÚ½ñÌìµÄ RDBMS Ìåϵ½á¹¹¶øÑÔ£¬ËÀËøÄÑÒÔ±ÜÃâ — ÔÚ¸ßÈÝÁ¿µÄ OLTP »·¾³ÖиüÊǼ«ÎªÆձ顣ÕýÊÇÓÉÓÚ .NET µÄ¹«¹²ÓïÑÔÔËÐпâ (CLR) µÄ³öÏÖ£¬ SQL Server 2005 ²ÅµÃÒÔΪ¿ª·¢ÈËÔ±ÌṩһÖÖеĴíÎó´¦Àí·½·¨¡£ÔÚ±¾ÔÂרÀ¸ÖУ¬ Ron Talmage ΪÄú½éÉÜÈçºÎʹÓà TRY/CATCH Óï¾äÀ´½â¾öÒ»¸öËÀËøÎÊÌâ¡£
Ò»¸öʾÀýËÀËø
ÈÃÎÒÃÇ´ÓÕâÑùÒ»¸öʾÀý¿ªÊ¼ËµÆð£¬ËüÔÚ SQL Server 2000 ºÍ 2005 Öж¼ÄÜÒýÆðËÀËø¡£ÔÚ±¾ÎÄÖУ¬ÎÒʹÓà SQL Server 2005 µÄ×îРCTP£¨ÉçÇø¼¼ÊõÔ¤ÀÀ£¬Community Technology Preview£©°æ±¾£¬SQL Server 2005 Beta 2£¨7 Ô·¢²¼£©Ò²Í¬ÑùÊÊÓá£Èç¹ûÄúûÓÐ Beta 2 »ò×îÐ嵀 CTP °æ±¾£¬ÇëÏÂÔØ SQL Server 2005 Express µÄ×îа汾£¬ÓÃËüÀ´½øÐÐÊÔÑé¡£
¿ÉÄÜ·¢ÉúµÄËÀËøÇé¿öÓкܶ࣬[ ²ÎÔÄ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_con_7a_3xrf.asp ÒÔ¼°ËÀËøÎÄÕÂÊ÷ÖеĺóÐøÎÄÕ¡£ — ±àÕß ]£¬µ«×îÓÐȤ¡¢×î΢ÃîµÄÊÇÄÇЩ¹ØÓÚÔĶÁÆ÷ºÍ±àдÆ÷»¥Ïà×èÈûµÄËÀËø¡£ÒÔÏ´úÂëÔÚ pubs Êý¾Ý¿âÖоͲúÉúÁËÕâÑùÒ»¸öËÀËø¡££¨Äú¿ÉÒÔÔÚ SQL Server 2000 µÄÁ½¸ö Query Analyzer ´°¿ÚÖлò SQL Server 2005 µÄÁ½¸ö Management Studio queries Öв¢ÁÐÔËÐÐÕâ¶Î´úÂë¡££©ÔÚÆäÖÐÒ»¸ö´°¿ÚÖеĴúÂëÕýÎÄÇ°ÃæÌí¼ÓÏÂÁÐÓï¾ä£º
-- Window 1 header
DECLARE @au_id varchar(11), @au_lname varchar(40)
SELECT @au_id = '111-11-1111', @au_lname = 'test1'
ÔÚµÚ¶þ¸ö´°¿ÚÖÐÌí¼ÓÏÂÁÐÓï¾ä£¬½øÐеڶþ´ÎÁ¬½Ó£º
-- Window 2 header
DECLARE @au_id varchar(11), @au_lname varchar(40)
SELECT @au_id = '111-11-1112', @au_lname = 'test2'
ÔÚÁ½¸ö´°¿ÚÖж¼Ê¹ÓÃÏÂÁÐÓï¾ä×÷Ϊ´úÂëÕýÎÄ£º
-- Body for both connections:
BEGIN TRANSACTION
INSERT Authors VALUES
(@au_id, @au_lname, '', '', '', '', '', '11111', 0)
WAITFOR DELAY '00:00:05'
SELECT *
from authors
WHERE au_lname LIKE 'Test%'
COMMIT
ÔÚµÚÈý¸ö´°¿ÚÖÐÔËÐÐÏÂÁÐÓï¾ä£¬È·±£ authors ±í¸ñÖÐûÓÐÈκΰüº¬ÒÔÏ id µÄÊý¾Ý£º
DELETE from authors WHERE au_id = '111-11-1111'
DELETE from authors WHERE au_id = '111-11-1112'
ÔÚ 5 ÃëÖÓÄÚͬʱִÐд°¿Ú 1 ºÍ ´°¿Ú 2¡£ÒòΪÿ¸ö´°¿Ú¶¼ÒªµÈ´ýÖÁÉÙ 5 ÃëÖÓµÄʱ¼ä²ÅÄÜ·¢³ö SELECT Óï¾ä£¬ËùÓÐÿ¸öÁ¬½Ó¶¼½«Íê³É INSERT ²Ù×÷£¬ÕâÑù¾Í±£Ö¤ÁËÁ½¸ö´°¿ÚÖÐµÄ INSERT ²Ù×÷ÔÚ¸÷×﵀ SELECT Óï¾ä·¢²¼Ç°¾ÍÒѾÍê³ÉÁË¡£Ã¿¸ö´°¿ÚÖÐµÄ S
Ïà¹ØÎĵµ£º
ÔÚ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 ......
value·½·¨
µ±Äã²»Ïë½âÊÍÕû¸ö²éѯµÄ½á¹û¶øÖ»ÏëµÃµ½Ò»¸ö±êÁ¿ÖµÊ±£¬Õâ¸övalue·½·¨ÊǺÜÓаïÖúµÄ¡£Õâ¸övalue·½·¨ÓÃÓÚ²éѯXML²¢ÇÒ·µ»ØÒ»¸öÔ×ÓÖµ¡£
Õâ¸övalue·½·¨µÄÓï·¨ÈçÏ£º
value(XQuery£¬datatype)
½èÖúÓÚvalue·½·¨£¬Äã¿ÉÒÔ´ÓXMLÖеõ½µ¥¸ö±êÁ¿Öµ¡£Îª´Ë£¬Äã±ØÐëÖ¸¶¨XQueryÓï¾äºÍÄãÏëÒªËü·µ»ØµÄÊý¾ÝÀàÐÍ£¬²¢ÇÒÄã¿ÉÒÔ·µ»Ø³ ......
Çå¿ÕÈÕÖ¾
1£®´ò¿ª²éѯ·ÖÎöÆ÷£¬ÊäÈëÃüÁî
DUMP TRANSACTION Êý¾Ý¿âÃû WITH NO_LOG
2.ÔÙ´ò¿ªÆóÒµ¹ÜÀíÆ÷--ÓÒ¼üÄãҪѹËõµÄÊý¾Ý¿â--ËùÓÐÈÎÎñ--ÊÕËõÊý¾Ý¿â--ÊÕËõÎļþ--Ñ¡ÔñÈÕÖ¾Îļþ--ÔÚÊÕËõ·½Ê½ÀïÑ¡ÔñÊÕËõÖÁXXM,ÕâÀï»á¸ø³öÒ»¸öÔÊÐíÊÕËõµ½µÄ×îСMÊý,Ö±½ÓÊäÈëÕâ¸öÊý,È·¶¨¾Í¿ÉÒÔÁË¡£
......
¿´ÁËһƪ½²×ù£¬Ëµµ½Êý¾Ý²ã·ÖÒ³¼¼Êõ£¬Óõ½ÁË4Öз½Ê½£¬1£©Ê¹ÓÃtop *top 2)ʹÓñí±äÁ¿ 3£©Ê¹ÓÃÁÙʱ±í 4£©Ê¹ÓÃROW_NUMBERº¯Êý¡£
ÆäÖÐ×î¿ìµÄÊǵÚ1 ºÍµÚ4Öз½Ê½£¬½ÓÏÂÀ´ÎÒÃÇÀ´¿´¿´ÕâÁ½ÖÖ·½Ê½£º
ÎÒÃÇʹÓÃsql2005×Ô´øµÄÊý¾Ý¿â AdventureWorks²âÊÔ£¬
1£©
--Use Top*Top
DECLARE @Start datetime,@end datetim ......