Rule Definition
Not returning a status value can lead to data corruption and incorrect behaviour.
Remediation
Always use the return value to return a status. By convention return 0 on success.
Fixed Code Sample
Declare @OpenedTran bit
Declare @Error int
/*See if a transaction is already opened and possibly open a new one*/
If @@trancount>0
Select @OpenedTran = 1
Else
Begin
Select @OpenedTran = 0
Begin Tran
End
update mytable set col1 = 123
where col2 = 456
Select @Error = @@error
GTRAN:
/*Close or rollback the transaction if it has been opened in this procedure*/
If @OpenedTran = 0
Begin
If @Error = 0 /* no error occurs*/
Commit Tran
Else
Rollback Tran
End
/*If this procedure is not called from another one, Raise an error*/
If @@nestlevel = 1
Begin
If @Error !=0
Raiserror xxx
End
/*return the error value*/
Return @Error
Related Technologies
Microsoft T-SQL
Sybase T-SQL
Technical Criterion
Programming Practices - Error and Exception Handling
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.