_覓 | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁 |
FrontPage › SQLServer2005T-SQL
|
|
覓語 譴...
[edit]
1 Data Type #蠍一ヾ varchar, nvarchar, varbinary 8000覦危語 螳譟. 蠏碁 2005 覯 max れ襯 2GB蟾讌 蟆 . XML 一危 襦 襷れ伎螻, 2GB蟾讌 .
Use AdventureWorks Go Create Table SalesData.Max_Varchar_Table( Max_Varchar Varchar(Max) , Max_nVarchar nVarchar(Max) , Max_varBinary varBinary(Max) ) Go Create Table SalesData.Max_XML_Table( Max_XML_Col XML) Go [edit]
3 Try ... Catch #伎 覯 語襴螳 譬 蟲碁 蟆 れ企. 企 蟆 覦 Try Catch 蟲覓語 蟆 覯碁. 蟲覓語 蠍 伎 If @@Error 貊螳 譯朱 蠍磯 覃, XACT_ABORT ON 旧 .
BEGIN TRY { sql_statement | statement_block } END TRY BEGIN CATCH { sql_statement | statement_block } END CATCH [ ; ] れ Try Catch 螳 企. 覓碁螻 螳 譴螳 Go 螳 ろ 覓語螳 れ願覃 .
BEGIN TRY SELECT * FROM sys.messages WHERE message_id = 21; END TRY -- GO -- The previous GO breaks the script into two batches, -- generating syntax errors. The script runs if this GO -- is removed. BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber; END CATCH; GO れ螻 螳 Error Message襯 訖れ .
USE AdventureWorks; GO BEGIN TRANSACTION; GO BEGIN TRY -- Generate a constraint violation error. DELETE FROM Production.Product WHERE ProductID = 980; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() as ErrorState, ERROR_MESSAGE() as ErrorMessage; END CATCH GO ROLLBACK TRANSACTION; GO Try Catch襯 伎 るジ 2螳讌 覦覯 螳襦 蟆. 豌 覯讌 覦覯 @@Error襯 伎 覦覯企. Insert 覓語螳 る Commit 螻, 覓語螳 覦覃 Catch襦 企 豬螻 . XACT_ABORT ON螻 @@Error螳 . @@Error 旧企, XACT_ABORT 覦 .
Use Tempdb Go CREATE TABLE dbo.DataTable (ColA int PRIMARY KEY, ColB int) CREATE TABLE dbo.ErrorLog (ColA int, ColB int, error int, date datetime) GO CREATE PROCEDURE dbo.AddData @a int, @b int AS SET XACT_ABORT ON BEGIN TRY BEGIN TRAN INSERT INTO dbo.DataTable VALUES (@a, @b) COMMIT TRAN END TRY BEGIN CATCH DECLARE @err int SET @err = @@error --trap the error number ROLLBACK TRAN INSERT INTO dbo.ErrorLog VALUES (@a, @b, @err, GETDATE()) END CATCH GO EXEC dbo.AddData 1, 1 EXEC dbo.AddData 2, 2 EXEC dbo.AddData 1, 3 --violates the primary key Select * From dbo.ErrorLog Go /* ColA ColB error date ----------- ----------- ----------- ----------------------- 1 3 2627 2005-02-12 13:54:54.377 (1 row(s) affected) */ 覯讌 蟆 覲 蟆 XACT_STATE()襯 伎 蟆企. -1 企 碁 企 伎語 覈襯伎襷 ろ 蟆 企, 1企 炎概企. 0企 讌 譴企. 企 蟆 伎 XACT_ABORT ON螻 COMMIT TRAN 伎 譯殊豌襴 るジ 覦覯 . 覓伎^蟇 Error螳 覦 蟆豌 螳ロ伎 覩襦 RAISERROR襯 蟆 覲 . RAISERROR 覦 .
Use Tempdb Go CREATE TABLE dbo.DataTable (ColA int PRIMARY KEY, ColB int) CREATE TABLE dbo.ErrorLog (ColA int, ColB int, error int, date datetime) GO CREATE PROCEDURE dbo.AddData @a int, @b int AS --SET XACT_ABORT ON BEGIN TRY BEGIN TRANSACTION; INSERT INTO dbo.DataTable VALUES (@a, @b) RAISERROR(N'Throw an error.', 16, 1); -- Need --COMMIT TRAN END TRY BEGIN CATCH DECLARE @err int IF (XACT_STATE()) = -1 ROLLBACK TRANSACTION; SET @err = @@error --trap the error number INSERT INTO dbo.ErrorLog VALUES (@a, @b, @err, GETDATE()) IF (XACT_STATE()) = 1 COMMIT TRANSACTION; END CATCH GO EXEC dbo.AddData 1, 1 EXEC dbo.AddData 2, 2 EXEC dbo.AddData 1, 3 --violates the primary key Select * From dbo.ErrorLog Go
鏤
|
螳 朱 蟲 豌 覺 讌朱 覦 螳襴襯 讌 . 覺 朱 覲旧蟒企 覯蟒 覿讌 . 襴 覿 ル瑚 企蟆 るジ 蟒覲企 蟆 朱 讌 . 蠏語 覓給牛 譴觜覃 願概 螻 訖企. 蠏碁り 襷る語襴 蠏語螻 讌 螳 蠍 谿襦螳 讌蠍 蟲 る 譴觜伎 蠏 リ鍵 襯 襷蟷 觸企 蟆企. (觜) |