Reputation: 468
I run a sql statement in a stored procedure like so:
...
CREATE PROCEDURE my_proc
AS
BEGIN
BEGIN TRAN
...do some work
EXEC msdb.dbo.sp_start_job N'MyJob';
...do some work, maybe rollback if some criteria is not met
COMMIT TRAN
...
END
Are the steps in 'MyJob' a part of this transaction? Or do they occur independently of it?
For a specific example: 'MyJob' deletes records from table 'MyData'. Then some logic in the stored proc ends up rolling back the transaction instead of committing it. Will the records in 'MyData' that were deleted by the job be deleted?
Upvotes: 1
Views: 910
Reputation: 95827
sp_start_job
starts the job, and then anything that job does is asynchronous. This means that they are in an entirely separate session. If you were to rollback the transaction, then the job still would have been started, and any actions the job performed will be unaffected.
Upvotes: 1