Question: Create the following SQL code in the query window. Comment each step in the script.! Transaction 1 Prep Data as shown below DECLARE @NewStudentID int;

Create the following SQL code in the query window. Comment each step in the script.!
Transaction 1
Prep Data as shown below
DECLARE @NewStudentID int;
Set @NewStudentID =999; -- initialized dummy value
DECLARE @CourseID int;
SET @CourseID =12;
Write the next 3 SQL statements as a transaction
INSERT Students VALUES ('Hamilton', 'Alexander', GETDATE(), NULL);
SET @NewStudentID = @@IDENTITY;
INSERT StudentCourses VALUES (@NewStudentID, @CourseID);
If the previous step is successful, commit the transaction. Otherwise, issue a rollback.
Prior to the Commit - Issue the command PRINT 'Commit of Student ID '+ CAST(@NewStudentID AS VARCHAR(10));
Prior to the Rollback - Issue the command PRINT 'Rollback for Alexander Hamilton'
Outside of the transaction - issue the command and inspect the results.
Select * from Students order by EnrollmentDate;
Note: This transaction should show the "Commit" message.
Transaction 2
Copy all the Code from Transaction 1. Make the following modifications to this copy.
Remove the copied/existing INSERT Statement for Alexander Hamilton.
Add the following 2 INSERT Statements in that place
INSERT Students VALUES ('Jackson', 'Andrew', GETDATE(), NULL);
Issue the SET @NewStudentID ... and INSERT StudentCourses ... lines
INSERT Students VALUES ('Burr', 'Aaron', GETDATE(),'2024-99-99');
Issue the SET @NewStudentID ... and INSERT StudentCourses ... lines
If the previous step is successful, commit the transaction. Otherwise, issue a rollback.
Prior to the Commit - Issue the command PRINT 'Commit of 2 of Students'
Prior to the Rollback - Issue the command PRINT 'Rollback for 2 Students'
Outside of the transaction - issue the command and inspect the results.
Select * from Students order by EnrollmentDate;
Note - this transaction should show ROLLBACK due to the invalid date in the Aaron Burr insert. Neither Burr or Andrew Jackson should be added

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!