0
I’m looking to use a trigger
to update the field of a table whenever there is an Insert in this same table.
The problem I’m having at Trigger is that the update doesn’t work, it follows 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 one, but it didn’t 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 already identified that the problem is in the update SET 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 the following: It is possible to use a function to set the field in an UPDATE command within a Rigger like I am doing?
And what is the data type of the function parameter
dbo.f_descriptionNFSe
?– José Diz