這是一個將TRANSACTION添加到我的代碼的好方法。我必須先更新我的代碼,如果失敗,那麼插入也應該不起作用。如果這是一種正確的方式,請看看。如果沒有,那麼請改善。這是將事務添加到sql的好方法嗎?
Begin Transaction[Transaction1]
Begin Try
IF(@ServiceInfoToJobStatus = 1)
Update ServiceInfo
Set ServiceInfoToJobStatus= 0 --To set all current jobs to prior because one person cannot have many jobs selected as current
Where @ServiceInfoToJobStatus = 1 AND ServiceInfo.fk_PersonalInfo_ServiceInfo_PID= @fk_PersonalInfo_ServiceInfo_PID
Set @ServiceInfoEntryDateTime= (Select GetDate())
Insert into dbo.ServiceInfo
(
ServiceInfoInitialDesignation,
ServiceInfoInitialBPS,
fk_Districts_ServiceInfo_InitialDistrictID,
ServiceInfoJobStatus,
ServiceInfoFromDate,
ServiceInfoDepartment,
fk_PersonalInfo_ServiceInfo_PID,
ServiceInfoServiceType ,
ServiceInfoOffice ,
ServiceInfoCadre ,
fk_WebUsers_ServiceInfo_UserID,
ServiceInfoEntryDateTime,
ServiceInfoToDesignation ,
ServiceInfoToBPS ,
fk_Districts_ServiceInfo_ToDistrictID ,
ServiceInfoToJobStatus ,
ServiceInfoToDate ,
ServiceInfoToDepartment ,
ServiceInfoToServiceType ,
ServiceInfoToOffice ,
ServiceInfoToCadre
)
Values
(
@ServiceInfoInitialDesignation,
@ServiceInfoInitialBPS,
@fk_Districts_ServiceInfo_InitialDistrictID,
@ServiceInfoJobStatus,
@ServiceInfoFromDate,
@ServiceInfoDepartment,
@fk_PersonalInfo_ServiceInfo_PID,
@ServiceInfoServiceType ,
@ServiceInfoOffice ,
@ServiceInfoCadre ,
@fk_WebUsers_ServiceInfo_UserID,
Convert(varchar, @ServiceInfoEntryDateTime, 113),
@ServiceInfoToDesignation ,
@ServiceInfoToBPS ,
@fk_Districts_ServiceInfo_ToDistrictID ,
@ServiceInfoToJobStatus ,
@ServiceInfoToDate ,
@ServiceInfoToDepartment ,
@ServiceInfoToServiceType ,
@ServiceInfoToOffice ,
@ServiceInfoToCadre
)
Set @ReturnStatus = 1
Commit Transaction[Transaction1]
End Try
Begin Catch
ROLLBACK Transaction[Transaction1]
Set @ReturnStatus= 0
Set @ReturnStatusMessage= (Select ERROR_MESSAGE())
End Catch