Saturday, December 26, 2020

T-SQL Transaction Most Basic Example

 BEGIN TRY

                BEGIN TRANSACTION

                                DECLARE @newID INT;

                               

                                --For testing, bad SQL goes here:

                                --INSERT INTO Intake(intake_id, name, pet) VALUES (39,'Ape', 'Dog')

                               

                                --Good SQL goes here:

                               

 

                                    INSERT INTO Intake(res_name) VALUE("joe");

                                              

                                               

                                SET @newID =scope_identity();

                               

                                INSERT INTO Intake_ISC_lutb(Intake_ID, ISC_Guid)VALUES(@newID, 'Smedly')

               

                COMMIT TRAN

END TRY

BEGIN CATCH

                IF @@TRANCOUNT > 0

                                ROLLBACK TRAN

                                --RAISERROR('Error raised in Intake', 16,1)

END CATCH





No comments: