Transactions and Error Handling in SQL Server
Miriam Antona
Software Engineer
| product_id | product_name | stock | price |
|------------|---------------------------------|-------|---------|
| 1 | Trek Conduit+ - 2016 | 50 | 2999.99 |
| 2 | Sun Bicycles ElectroLite - 2017 | 47 | 1559.99 |
| 3 | Trek Powerfly 8 FS Plus - 2017 | 41 | 4999.99 |
| 4 | Trek Conduit+ - 2018 | 10 | 2799.99 |
| 5 | Trek CrossRip+ - 2018 | 12 | 4499.99 |
| buyer_id | first_name | last_name | email | phone |
|----------|------------|-----------|-----------------------|-----------|
| 1 | Dylan | Smith | [email protected] | 555888999 |
| 2 | John | Antona | [email protected] | 555111222 |
| 3 | Astrid | Harper | [email protected] | 555000999 |
| 4 | Angus | Brown | [email protected] | 555222012 |
| 5 | David | Elcano | [email protected] | 555602314 |
| staff_id | first_name | last_name | email | phone |
|----------|------------|-----------|-------------------------|-----------|
| 1 | Mohammed | Ferrec | [email protected] | 555888111 |
| 2 | Dimitri | Brown | [email protected] | 555012012 |
| 3 | Leila | Merheg | [email protected] | 555999133 |
| 4 | Mateo | Casanovas | [email protected] | 555110996 |
| 5 | Carl | York | [email protected] | 555010011 |
| order_id | product_id | buyer_id | staff_id | price | order_date |
|----------|------------|----------|----------|-------|------------|
| 1 | 2 | 1 | 5 | 100 | 1559.99 |
| 2 | 2 | 5 | 5 | 100 | 1559.99 |
| 3 | 5 | 10 | 1 | 100 | 4499.99 |
| 4 | 10 | 3 | 3 | 100 | 2799.99 |
| 5 | 15 | 2 | 7 | 100 | 2299.99 |
| product_id | product_name | stock | price |
|------------|-------------------------------------|-------|---------|
| 19 | Trek Powerfly 5 - 2018 | 15 | 3499.99 |
CONSTRAINT unique_product_name UNIQUE (product_name);
INSERT INTO products (product_name, stock, price)
VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
Violation of UNIQUE KEY constraint 'unique_product_name'.
Cannot insert duplicate key in object 'dbo.products'.
The duplicate key value is (Trek Powerfly 5 - 2018).
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
[ { sql_statement | statement_block } ]
END CATCH
[ ; ]
TRY
blockCATCH
blockTRY
block -> the CATCH
block takes the controlTRY
block -> the CATCH
block is skippedBEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
SELECT 'Product inserted correctly!' AS message;
END TRY
BEGIN CATCH
SELECT 'An error occurred! You are in the CATCH block' AS message;
END CATCH
| message |
|-----------------------------------------------|
| An error occurred! You are in the CATCH block |
BEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES ('Super new Trek Powerfly', 5, 1499.99);
SELECT 'Product inserted correctly!' AS message;
END TRY
BEGIN CATCH
SELECT 'An error occurred! You are in the CATCH block' AS message;
END CATCH
| message |
|-----------------------------|
| Product inserted correctly! |
BEGIN TRY
INSERT INTO products (product_name, stock, price)
VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
SELECT 'Product inserted correctly!' AS message;
END TRY
BEGIN CATCH
SELECT 'An error occurred inserting the product!
You are in the first CATCH block' AS message;
BEGIN TRY
INSERT INTO myErrors
VALUES ('ERROR!');
SELECT 'Error inserted correctly!' AS message;
END TRY
BEGIN CATCH
SELECT 'An error occurred inserting the error!
You are in the second CATCH block' AS message;
END CATCH
END CATCH
| message |
|-------------------------------------------|
| An error occurred inserting the product! |
| You are in the first CATCH block |
| message |
|-------------------------------------------|
| An error occurred inserting the error! |
| You are in the second CATCH block |
Transactions and Error Handling in SQL Server