I need to create an error-handling method for a couple of Stored Procedures.
Proc1
CREATE PROCEDURE dbo.Proc1
AS
BEGIN TRY
BEGIN TRANSACTION
EXEC Proc2
INSERT INTO tableA (col1, col2) values (1,GETDATE())
--do something that causes error
SELECT 1/0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
Proc2
CREATE PROCEDURE dbo.Proc2
AS
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO tableB (col1,col2,col3) values (1,'John Doe',53)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
The reason I need nested try/catch transactions
is that Proc2 can be called without proc1, but proc1 will always call proc2.
My question is, will that insert into tableB
be rolled back? If not, how can I make it roll back?