I’m trying to use the TRY/CATCH method when deleteing a record. If the record exists, it works. But if the record does not exist? The following doesn’t tell me that the record does not exist and I do not receive any error message. Please note the attempt with the IF/ELSE.
TIA.
Bill
Code:
BEGIN TRY
BEGIN TRANSACTION
/*
IF EXISTS(SELECT * FROM tbl_Admin_BatchNumbers
where BatchNumber = @BatchNumber)
BEGIN
*/
Delete from tbl_Admin_BatchNumbers
where tbl_Admin_BatchNumbers.BatchNumber =
@BatchNumber
--If we reach here, success!
COMMIT
--end
/*
ELSE --Batch number does not exist. tell user. Rollback.
Begin
set @Message = 'Batch Number Does Not Exist.'
rollback
End
*/
END TRY
BEGIN CATCH
-- Whoops, there was an error
IF @@TRANCOUNT > 0
ROLLBACK
-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
--Keep the RAISERROR for ASP use
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
TIA.
Bill