.net - T-SQL Throw Exception within a transaction -
today trying write transaction, within stored procedure in returns different numbers. necessary c# code know error. problem is, if transaction not completed throws same exception.
i not throw exception inside transaction, giving wrong syntax. trying pattern:
throw 51000, 'the record not exist.', 1; is possible make this:
begin tran try if... throw error 4; if.. throw error 2; complete tran; catch return error number; i need specific number of error returned stored procedure.
can please give me example of that?
p.s. forgot version of our sql server, no i'm not in office. either 2008 or 2012.
you can use following syntax:
begin try begin transaction if ... raiserror(51001, 16, 1) if ... raiserror(51002, 16, 1) commit transaction end try begin catch declare @errornumber int = error_number(); declare @errorline int = error_line(); declare @errormessage nvarchar(4000) = error_message(); declare @errorseverity int = error_severity(); declare @errorstate int = error_state(); if @@trancount > 0 rollback transaction; raiserror(@errormessage, @errorseverity, @errorstate); end catch error number should greater 50000. can add sp_addmessage procedure. throw introduced in sql server 2012. there differences. can find them in msdn.
Comments
Post a Comment