Question: 2. 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/2013',

2. 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/2013', 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 thats automatically generated when the first INSERT statement inserts an order. If these statements execute successfully, commit the changes. Otherwise, roll back the changes.

My code:

BEGIN INSERT Orders VALUES (3, GETDATE(), '10.00', '0.00', NULL, 4, 'American Express', '378282246310005', '04/2013', 4); SET @OrderID = @@IDENTITY; INSERT OrderItems VALUES (@OrderID, 6, '415.00', '161.85', 1); INSERT OrderItems VALUES (@OrderID, 1, '699.00', '209.70', 1); COMMIT; PRINT 'The transaction was commited.'; EXCEPTION WHEN OTHERS THEN ROLLBACK; PRINT 'The transaction was rolled back.'; END;

and the errors i am getting:

Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'BEGIN'. Msg 137, Level 15, State 1, Line 8 Must declare the scalar variable "@OrderID". Msg 137, Level 15, State 2, Line 10 Must declare the scalar variable "@OrderID". Msg 137, Level 15, State 2, Line 12 Must declare the scalar variable "@OrderID". Msg 102, Level 15, State 1, Line 19 Incorrect syntax near ';'.

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 Databases Questions!