22 - Transactions and Error Handling in SQL Server
22 - Transactions and Error Handling in SQL Server
-- error-handling script
BEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES ('Sun Bicycles ElectroLite - 2017', 10, 1559.99);
END TRY
BEGIN CATCH
SELECT 'An error occurred inserting the product!';
BEGIN TRY
INSERT INTO errors
VALUES ('Error inserting a product');
END TRY
BEGIN CATCH
SELECT 'An error occurred inserting the error!';
END CATCH
END CATCH
-- Using error functions
BEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES ('Trek Powerfly 5 - 2018', 2, 3499.99),
('New Power K- 2018', 3, 1999.99)
END TRY
-- Set up the outer CATCH block
BEGIN CATCH
SELECT 'An error occurred inserting the product!';
-- Set up the inner TRY block
BEGIN TRY
-- Insert the error
INSERT INTO errore
VALUES ('Error inserting a product');
END TRY
-- Set up the inner CATCH block
BEGIN CATCH
-- Show number and message error
SELECT
ERROR_LINE() AS line,
ERROR_MESSAGE() AS message;
END CATCH
END CATCH
/*
Raising, throwing and customizing your errors
*/
BEGIN TRY
DECLARE @product_id INT = 5;
IF NOT EXISTS (SELECT * FROM products WHERE product_id = @product_id)
RAISERROR('No product with id %d.', 11, 1, @product_id);
ELSE
SELECT * FROM products WHERE product_id = @product_id;
END TRY
-- Catch the error
BEGIN CATCH
-- Select the error message
SELECT ERROR_MESSAGE();
END CATCH
AS
BEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES (@product_name, @stock, @price);
END TRY
-- Set up the CATCH block
BEGIN CATCH
-- Insert the error and end the statement with a semicolon
INSERT INTO errors VALUES ('Error inserting a product');
-- Re-throw the error
THROW;
END CATCH
BEGIN TRY
-- Execute the stored procedure
EXEC insert_product
-- Set the values for the parameters
@product_name = 'Super bike',
@stock = 3,
@price = 499.99;
END TRY
-- Set up the CATCH block
BEGIN CATCH
-- Select the error message
SELECT error_message();
END CATCH
-- Set @staff_id to 4
DECLARE @staff_id INT = 4;
EXEC sp_addmessage @msgnum = 50002, @severity = 16, @msgtext = 'There are not
enough %s bikes. You only have %d in stock.', @lang = N'us_english';
/*
Transactions in SQL Server
*/
-- Correcting a transaction
BEGIN TRY
begin TRAN
UPDATE accounts SET current_balance = current_balance - 100 WHERE
account_id = 1;
INSERT INTO transactions VALUES (1, -100, GETDATE());
BEGIN TRY
-- Begin the transaction
BEGIN transaction;
UPDATE accounts SET current_balance = current_balance - 100 WHERE
account_id = 1;
INSERT INTO transactions VALUES (1, -100, GETDATE());
BEGIN TRY
-- Begin the transaction
BEGIN TRAN;
-- Correct the mistake
UPDATE accounts SET current_balance = current_balance + 200
WHERE account_id = 10;
-- Check if there is a transaction
IF @@TRANCOUNT > 0
-- Commit the transaction
COMMIT TRAN;
-- Using savepoints
BEGIN TRAN;
-- Mark savepoint1
SAVE TRAN savepoint1;
INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]',
'555909090');
-- Mark savepoint2
SAVE TRAN savepoint2;
INSERT INTO customers VALUES ('Zack', 'Roberts', '[email protected]',
'555919191');
-- Rollback savepoint2
ROLLBACK TRAN savepoint2;
-- Rollback savepoint1
ROLLBACK TRAN savepoint1;
-- Mark savepoint3
SAVE TRAN savepoint3;
INSERT INTO customers VALUES ('Jeremy', 'Johnsson',
'[email protected]', '555929292');
-- Commit the transaction
COMMIT TRAN;
-- Doomed transactions
/*
Controlling the concurrency: Transaction isolation levels
*/
-- Begin a transaction
begin tran
-- Begin a transaction
begin tran
SELECT *
-- Avoid being blocked
FROM transactions with (nolock)
WHERE account_id = 1