Write a script that includes these statements coded as a transaction:
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Chapter 16
How to manage transactions
and locking
Excercises using the My Guitar Shop
Use Microsoft SQL Server
- Write a script that includes these statements coded as a transaction:
INSERT Orders
VALUES (3, GETDATE(), '10.00', '0.00', NULL, 4,
'American Express', '378282246310005', '04/2019', 4);
SET @OrderID = @@IDENTITY;
INSERT OrderItems
VALUES (@OrderID, 6, '415.00', '161.85', 1);
INSERT OrderItems
VALUES (@OrderID, 1, '699.00', '209.70', 1);
Here, the @@IDENTITY variable is used to get the order ID value that’s automatically generated when the first INSERT statement inserts an order.
If these statements execute successfully, commit the changes. Otherwise, roll back the changes.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps

Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question
I am getting this error with the EXCEPTION

Transcribed Image Text:### SQL Transaction Script
This snippet is an SQL script demonstrating the use of transactions with error handling. The script performs a sequence of database operations, inserting data into two tables: `Orders` and `OrderItems`.
**Code Breakdown:**
- **BEGIN;**
Initializes a transaction block.
- **DECLARE @OrderID INT**
Declares a variable `@OrderID` of type integer.
- **INSERT Orders**
Inserts a new record into the `Orders` table with the specified values. It uses the `GETDATE()` function for the current date and the `@@IDENTITY` function to retrieve the last inserted identity value to store in `@OrderID`.
```sql
VALUES (3, GETDATE(), '10.00', '0.00', NULL, 4, 'American Express', '378282246310005', '04/2019', 4);
```
- **INSERT OrderItems**
Inserts two new records into the `OrderItems` table, using the previously retrieved `@OrderID` for referential integrity.
```sql
VALUES (@OrderID, 6, '415.00', '161.85', 1);
VALUES (@OrderID, 1, '699.00', '209.70', 1);
```
- **COMMIT**
Commits the transaction if all operations are successful, printing a success message.
- **PRINT 'The transaction was committed.';**
Outputs a message indicating the transaction was successful.
- **EXCEPTION Handling Section**
This part is intended to handle any errors that occur during the transaction.
```sql
WHEN OTHERS THEN ROLLBACK;
PRINT 'The transaction was rolled back.';
```
Note: In T-SQL (SQL Server), the correct syntax for error handling uses `BEGIN TRY...END TRY` and `BEGIN CATCH...END CATCH` blocks. The `EXCEPTION` keyword and `WHEN OTHERS THEN` are not valid in T-SQL, which results in a syntax error.
**Error Message:**
- **Msg 102, Level 15, State 1, Line 67**
Indicates a syntax error near the `EXCEPTION` keyword. This part of the code needs correction to properly handle exceptions.
**Suggestions for Correction:**
- Replace the `EXCEPTION` block with T-S
Solution
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education