Is it safe to Insert and use the inserted id in a single transaction
Like below.
What I want to know is when will be the new entry to the Table1
will be created? After committing the transaction or before?
will that Id will can be able to access in the Table2?
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO dbo.Table1([Name])
values('Name')
DECLARE @Id int
SET @Id = SCOPE_IDENTITY()
INSERT INTO dbo.TableNam2(UId,FeatureId)
VALUES(NEWID (), @Id)
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH