Académique Documents
Professionnel Documents
Culture Documents
06 | Modifying Data
INSERT, UPDATE, and DELETE statements, use of defaults, constraints, and triggers, OUTPUT
Using T-SQL programming elements, implementing error handling, understanding and implementing transactions
Querying system catalogs and dynamic management views, creating and executing stored procedures, improving SQL
Server query performance
--Valid batch
INSERT INTO Production.UnitMeasure (Name,
UnitMeasureCode, ModifiedDate)
VALUES (N'Square Footage', NF4', GETDATE()),
(N'Square Inches', NI2', GETDATE());
GO
--Invalid batch
INSERT INTO dbo.t1 VALUE(1,2,N'abc');
INSERT INTO dbo.t1 VALUES(2,3,N'def');
GO
Property
Function to Query
Description
Number
ERROR_NUMBER
Message
ERROR_MESSAGE
Severity
ERROR_SEVERITY
Procedure Name
ERROR_PROCEDURE
Line Number
ERROR_LINE
BEGIN TRY
-- Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
BEGIN TRY
-- Table does not exist; object name resolution
-- error not caught.
SELECT * FROM IDontExist;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
BEGIN TRY
SELECT 100/0 AS 'Problem';
END TRY
BEGIN CATCH
PRINT 'Code inside CATCH is beginning'
PRINT MyError: ' + CAST(ERROR_NUMBER()
AS VARCHAR(255));
THROW;
END CATCH
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Sales.SalesOrderHeader... --Succeeds
INSERT INTO Sales.SalesOrderDetail... --Fails
COMMIT TRANSACTION -- If no errors, transaction completes
END TRY
BEGIN CATCH
--Inserted rows still exist in Sales.SalesOrderHeader
SELECT ERROR_NUMBER()
ROLLBACK TRANSACTION --Any transaction work undone
END CATCH;
BEGIN TRY
BEGIN TRANSACTION -- marks beginning of transaction
INSERT INTO Sales.SalesOrderHeader... -Completed
INSERT INTO Sales.SalesOrderDetail... -Completed
...
BEGIN TRY
BEGIN TRAN -- marks beginning of transaction
INSERT INTO Sales.SalesOrderHeader...
INSERT INTO Sales.SalesOrderDetail...
COMMIT TRAN -- mark the transaction as complete
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() --sample error handling
ROLLBACK TRAN
END CATCH;
2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in
the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because
Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information
provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.