Write a script that includes these statements coded as a transaction: INSERT Orders VALUES (3, GETDATE(), '10.00',
Question:
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 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.
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 ';'.
Economics of Strategy
ISBN: 978-1118319185
6th edition
Authors: David Besanko, David Dranove, Mark Shanley, Scott Schaefer