Error in more than one transaction or statement in sql can be handled using Transaction in T-SQL.
example:
begin TRANsaction
IF(@Running=1)
BEGIN
UPDATE tblFiscalyearMaster SET
Running=0
,ModifiedBy = @CreatedBy
,ModifiedDate = GETDATE()
END
INSERT INTO tblFiscalyearMaster(FiscalyearID,FiscalyearName,FiscalyearCode,StartDate,EndDate,Running,Remarks,CreatedBy)
VALUES(@FiscalyearID,@FiscalyearName,@FiscalyearCode,@StartDate,@EndDate,@Running,@Remarks,@CreatedBy)
IF(@@ERROR<>0)
begin
ROLLBACK
return
end
COMMIT
end
In this Example,
Begin Transaction is the starting of the transaction and all the statements and queries are executed between begin TRANsaction and IF(@@ERROR<>0).
If statement check for error in the statements in between. if found ROLLBACK Rolls back to the previous state cancelling all the changes coused by the in between queries.
Finally , if no errors are found COMMIT statement finalizes the Transaction.
This, is an easy method of achieving consistency of data or error management in sql.
example:
begin TRANsaction
IF(@Running=1)
BEGIN
UPDATE tblFiscalyearMaster SET
Running=0
,ModifiedBy = @CreatedBy
,ModifiedDate = GETDATE()
END
INSERT INTO tblFiscalyearMaster(FiscalyearID,FiscalyearName,FiscalyearCode,StartDate,EndDate,Running,Remarks,CreatedBy)
VALUES(@FiscalyearID,@FiscalyearName,@FiscalyearCode,@StartDate,@EndDate,@Running,@Remarks,@CreatedBy)
IF(@@ERROR<>0)
begin
ROLLBACK
return
end
COMMIT
end
In this Example,
Begin Transaction is the starting of the transaction and all the statements and queries are executed between begin TRANsaction and IF(@@ERROR<>0).
If statement check for error in the statements in between. if found ROLLBACK Rolls back to the previous state cancelling all the changes coused by the in between queries.
Finally , if no errors are found COMMIT statement finalizes the Transaction.
This, is an easy method of achieving consistency of data or error management in sql.