I have doubts that my SP is correct since I don't know if it should be used only once begin try
and those instructions, apart from that, it returns a 1 but it doesn't do the update
:
ALTER PROCEDURE [dbo].[SP_EliminarVariableFormula]
@IdVariableElimina int, @ValorElimina varchar(100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Activo bit = 0
if @ValorElimina <> ''
BEGIN TRY
BEGIN TRAN
UPDATE ValoresDeVariables SET Activo=@Activo WHERE IDVariable=@IdVariableElimina AND ValorDeVariable=@ValorElimina
COMMIT TRAN
SELECT 1
END TRY
BEGIN CATCH
ROLLBACK TRAN
SELECT 0, 'Hubo un error.'
END CATCH
else
BEGIN TRY
BEGIN TRAN
UPDATE ValoresDeVariables SET Activo=@Activo WHERE IDVariable=@IdVariableElimina
COMMIT TRAN
SELECT 1
END TRY
BEGIN CATCH
ROLLBACK TRAN
SELECT 0, 'Hubo un error.'
END CATCH
END
Simplifying a bit, your procedure is something like this:
Therefore your
updates
are exclusive, or one is executed or the other is executed. Never both. Now, why would we need to use a transaction in this case?The usual use of a transaction is to provide consistency in an operation that minimally involves at least two statements, suppose you had to update
ValoresDeVariables
but before, insert a record in a second table, for exampleValoresDeVariablesLog
to record the value before the update, again, it is just an example.In this case, imagine that what we need is for the two operations to be atomic, that is, for both to work or neither for the operation to be consistent. Without a transaction, which could happen eventually, the first one
insert
works and theupdate
last one doesn't. That is exactly what we did not want at first and that is what transactions are for, if any statement fails, we will execute aROLLBACK
if on the contrary everything works, we will do aCOMMIT
, there are several ways to build a transactioned statement, this is one of them, using theTRY/CATCH
:For clarification, a transaction is normally rolled back on a database error, which is what we're doing with
TRY/CATCH
it however it could also be rolled back by business logic, but this is obviously handled differently as you don't have an error but a state/condition that you should eventually check.Now back to the original question: why would we need to use a transaction in this case? The answer: in this case it doesn't make sense . Why? Well, we have already seen that there are two
update
exclusive statements, and the other important data is that every statement ofupdate/insert/delete
is atomic, that is, it is executed completely or it does not. It could never be the case that any of these operations are executed halfway. Implicitly when you do:or the other statement, the engine handles it as if it were a transaction, so indicating it explicitly is redundant.
Clarification : I won't detail it so as not to extend myself, but a transaction can also manage the atomicity of the data that is read, but it does not seem to be the case in this example either.