Trigger condition

0

About the following trigger:

CREATE TRIGGER [dbo].[TG_TESTE] 
   ON  [MINHABASE].[dbo].[TB_DOCUMENTOS] 
   AFTER UPDATE
AS 

DECLARE @ID INT
DECLARE @DOC INT
DECLARE @QTD FLOAT

SELECT TOP 1 @ID = ID, @DOC = DOCUMENTO, @QTD = QUANTIDADE
FROM dbo.TB_DOCUMENTOS 
WHERE OPERACAO = 177
AND STATUS = 1
ORDER BY ID DESC

-- CONDICAO

BEGIN

SET NOCOUNT ON;

UPDATE dbo.TB_DOCUMENTOS
SET PESO = 1000
WHERE ID = @ID

END

In the query, I make the filter and set the variables @ID, @DOC, @QTD .

When this query DOES NOT return a record, will the trigger run update within BEGIN , even if I do not have a condition above?

    
asked by anonymous 24.04.2018 / 13:41

2 answers

1

You can use a conditional to ensure the integrity of the code, in this case it will not run the update but will try, which will generate a process ...

IF @ID != '' THEN
UPDATE dbo.TB_DOCUMENTOS
  SET PESO = 1000
  WHERE ID = @ID
END IF
    
24.04.2018 / 15:33