Example scenario:
I have the table / fields:
-
documentos
-
id
-
documento
-
pessoa
-
item
-
qtd
-
valor
-
The system gives INSERT
and then UPDATE
in this table in a continuous sequence (which would not be the case).
What would you like to see:
When you do UPDATE
, check some parameters and if the condition is true, the trigger makes another UPDATE
after.
What I did:
ALTER TRIGGER [dbo].[TG_TEST1]
ON [CORP].[dbo].[DOCUMENTOS]
AFTER UPDATE
AS
DECLARE @DOC_ID INT
DECLARE @DOC_QTD FLOAT
SELECT TOP 1 @DOC_ID = ID, @DOC_QTD = QTD
FROM dbo.DOCUMENTOS
WHERE PESSOA = 3
AND ITEM = 1
ORDER BY ID DESC
IF (@DOC_ID IS NOT NULL)
BEGIN
SET NOCOUNT OFF;
UPDATE dbo.DOCUMENTOS
SET QTD = (@DOC_QTD * 1000)
WHERE ID = @DOC_ID
END
Problem:
The problem is that I believe that SELECT TOP 1
is causing some internal problem in the application (which should not be post, since it is a specific application).
I saw that it has INSERTED
and DELETED
, but there is no UPDATED
that I can already get the record that has changed.
Doubt:
I'd like to know, how would I get the ID
of the record being done UPDATE
, plus other data to filter some values and make the condition if the other UPDATE
or not?