Link to home
Create AccountLog in
Avatar of Fred Webb
Fred WebbFlag for United States of America

asked on

Error when creating an UPDATE Trigger in SQL

I am trying to create a trigger on a table (RM00102) when Column (USERDEF1) is updated so it will add the current date to Table (SSGRM001) column (CustUpdDate). but when I fire the trigger with a change to USERDEF1 i get the following error
"a save operation on table RM_Customer_MSTR_ADDR failed because a database constraint was violated"

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trg_SSGRM001_CUSTUPD] ON [dbo].[RM00102] 
	AFTER UPDATE 
AS 
set nocount on 
IF UPDATE(USERDEF1)
BEGIN 
UPDATE D
SET D.CustUpdDate = GETDATE() 
FROM   RM00102 A INNER JOIN
SSGRM001 D ON A.CUSTNMBR = D.CUSTNMBR 
END 
set nocount off
GO

Open in new window

Avatar of JesterToo
JesterToo
Flag of United States of America image

That error message does not appear to originate from this trigger... what is "RM_Customer_MSTR_ADDR" and how does it relate to this transaction?
Avatar of Fred Webb

ASKER

"RM_Customer_MSTR_ADDR"  is the name used in Dynamics GP for table RM00102
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I was using SET D.CustUpdDate = GETDATE() but should have been using
 SET SSGRM001.CustUpdDate = (SELECT  DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) AS Expr1)
I missed that.
Avatar of Scott Pletcher
Or just CAST it as date:

SET D.CustUpdDate = CAST(GETDATE()  AS date)
Scott, Yeah... that would be less complicated, thanks