6
Example scenario:
I have the table/fields:
- documentos- id
- documento
- pessoa
- item
- qtd
- valor
 
The system gives INSERT and then UPDATE on this table in a continuous sequence (which would not come to the case).
What I’d like is:
When you do the UPDATE, check some parameters and if the condition is true, the Trigger do another UPDATE afterward.
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 SELECT TOP 1 is causing some internal problem in the application (which should not be posted, as it is a specific application).
I saw you have the INSERTED and DELETED, but there is no UPDATED that I can already pick up the record that was changed.
Doubt:
I’d like to know, how would I get the ID of the record being made the UPDATE, in addition to other data to filter some values and make the condition whether it will be realized the other UPDATE or not?
In the change you have the condition, then it would be the case to make a select before the
updateand control what you need to do now with the correct id? (I understand that this is not the point of the question, and if you are not having trouble getting around it, you can ignore the question =p)– rLinhares
@rLinhares No, I have a condition for the update done, and performed another update if the condition is true. I think that’s what you’re trying to say! rs
– rbz