Error Handling Quiz Week: Tryin’ TRY/CATCH


Let’s say we have two tables, Parent and Child, and we need to guarantee that they both get populated at once. We’ll write a single stored procedure to do both inserts:

DROP TABLE IF EXISTS dbo.Parent; DROP TABLE IF EXISTS dbo.Child; CREATE TABLE dbo.Parent (ID INT IDENTITY(1,1), DateAdded DATETIME2); CREATE TABLE dbo.Child (ID INT IDENTITY(1,1), DateAdded DATETIME2); GO CREATE OR ALTER PROC dbo.Insert_Two AS BEGIN INSERT INTO dbo.Parent(DateAdded) VALUES (GETDATE()); WAITFOR DELAY ’00:00:30′; INSERT INTO dbo.Child(DateAdded) VALUES (GETDATE()); END GO

I put a WAITFOR in there, but that isn’t the problem – I’m just using that to demonstrate why the code