sql server - writing a transaction in t-sql and error handling -


U think there is a better way to write a transaction in T-SQL? ? Email receiver under specified subject - ==========: Description - There is a better view that maintains and that uses this transaction

  Application performance is better =================================== ALTER PROCESS [CONTACT]. [Receiver_stpInsert] @First_Name nvarchar (30), @Last_Name nvarchar (30), @ Email Marketing (60), Get started as @Subject_Id Int session note; DECLARE @error_num int; Start transactions INSERT INTO Contact.Receiver (FIRST_NAME, LAST_NAME, email) prices (@First_Name, @Last_Name, @Email); SET @error_num = @@ error; IF (@error_num & lt;> 0) BEGIN Rollback; Return; End DECLARE @rec_record_id int; SET @rec_record_id = (contact record_id contact. Receiver where email = @ email); SET @error_num = @@ error; IF (@error_num & lt;> 0) BEGIN Rollback; Return; End INSERT INTO Contact.Receiver_Subject (Receiver_Id, Subject_Id) values ​​(@rec_record_id, @Subject_Id); SET @error_num = @@ error; IF (@error_num & lt;> 0) BEGIN Rollback; Return; END SET @error_num = @@ error; IF (@error_num & lt;> 0) BEGIN Rollback; Return; Start at the end; If you are using SQL 2005 or later, you can use the block. , Like this:    BEGIN Transactions in the beginning; INSERT INTO Contact.Receiver (FIRST_NAME, LAST_NAME, email) prices (@First_Name, @Last_Name, @Email); ... other inserts etc ... transfer contract; Finally catch if @@ transcount & gt; 0 rollback transactions; End catches;  

In this way, you keep repeating blocks similar to code @@ error If you want to know how the error is, you can get different bits of information in the BEGIN catch block:

  • Returns the number of errors.
  • ERROR_SEVERITY () gives seriousness.
  • Returns the error state number.
  • ERROR_PROCEDURE () returns the stored procedure name or triggers when the error occurs.
  • ERROR_LINE () returns the line number inside the routine, causing an error.
  • ERROR_MESSAGE () Returns the full text of the error message The text contains values ​​for any replacement parameters, such as length, object name, or time.

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -