豐富的數據類型 Richer Data Types
1、varchar(max)、nvarchar(max)和varbinary(max)數據類型最多可以保存2GB的數據,可以取代text、ntext或image數據類型。
CREATE TABLE myTable
(
id INT,
content VARCHAR(MAX)
)
2、XML數據類型
XML數據類型允許用戶在SQL Server數據庫中保存XML片段或文檔。
錯誤處理 Error Handling
1、新的異常處理結構
2、可以捕獲和處理過去會導致批處理終止的錯誤。前提是這些錯誤不會導致連接中斷(通常是嚴重程度為21以上的錯誤,例如,表或數據庫完整性可疑、硬件錯誤等等。)。
3、TRY/CATCH 構造
SET XACT_ABORT ON
BEGIN TRY
<core logic>
END TRY
BEGIN CATCH TRAN_ABORT
<exception handling logic>
END TRY
@@error may be quired as first statement in CATCH block
4、演示代碼
USE demo
GO
--創建工作表
CREATE TABLE student
(
stuid INT NOT NULL PRIMARY KEY,
stuname VARCHAR(50)
)
CREATE TABLE score
(
stuid INT NOT NULL REFERENCES student(stuid),
score INT
)
GO
INSERT INTO student VALUES (101,'zhangsan')
INSERT INTO student VALUES (102,'wangwu')
INSERT INTO student VALUES (103,'lishi')
INSERT INTO student VALUES (104,'maliu')
--調用一個運行時錯誤
SET XACT_ABORT OFF
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* 外鍵錯誤 */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
GO
SELECT * FROM student
SELECT * FROM score
--使用TRY...CATCH構造,並調用一個運行時錯誤
SET XACT_ABORT OFF
BEGIN TRY
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* 外鍵錯誤 */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
PRINT '事務提交'
END TRY
BEGIN CATCH
ROLLBACK
PRINT '事務回滾'
SELECT ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_MESSAGE() as ErrorMessage;
END CATCH
GO
SELECT * FROM score
GO