I want to use a trigger
to update the field of a table whenever there is an insert in this table.
The problem I'm having in the trigger is that the update does not work, it follows the trigger:
CREATE TRIGGER descNF ON tabela1
AFTER INSERT AS
BEGIN
DECLARE @IDMOV VARCHAR(MAX)
SET @IDMOV = (SELECT IDMOV FROM INSERTED)
UPDATE tabela1 SET tabela1.HISTORICOLONGO = dbo.f_descricao(@IDMOV) WHERE IDMOV = @IDMOV
END
I've tried to make some changes like this, but it did not work:
CREATE TRIGGER descNF ON tabela1
AFTER INSERT AS
BEGIN
DECLARE @IDMOV VARCHAR(MAX)
SET @IDMOV = (SELECT IDMOV FROM INSERTED)
DECLARE @TEXTO VARCHAR(MAX)
SET @TEXTO = dbo.f_descriptionNFSe(@IDMOV)
UPDATE tabela1 SET tabela1.HISTORICOLONGO = @TEXTO WHERE IDMOV = @IDMOV
END
I have already identified that the problem is in the SET of update with a function, because when I do this:
CREATE TRIGGER descNF ON tabela1
AFTER INSERT AS
BEGIN
DECLARE @IDMOV VARCHAR(MAX)
SET @IDMOV = (SELECT IDMOV FROM INSERTED)
UPDATE tabela1 SET tabela1.HISTORICOLONGO = 'TESTE' WHERE IDMOV = @IDMOV
END
It works and performs the update.
Removing the trigger update and running as a common UPDATE, as below:
DECLARE @IDMOV VARCHAR(MAX)
SET @texto = dbo.f_descriptionNFSe(123)
UPDATE tabela1 SET tabela1.HISTORICOLONGO = @texto WHERE IDMOV = 123
OU
UPDATE tabela1 SET tabela1.HISTORICOLONGO = dbo.f_descriptionNFSe(123) WHERE IDMOV = 123
It also works, so my question is this: can I use a function to set the field in an UPDATE command within an equal trigger I'm doing?