Error when creating an UPDATE Trigger in SQL

skull52
skull52 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
skull52IT director

Author

Commented:
"RM_Customer_MSTR_ADDR"  is the name used in Dynamics GP for table RM00102
Please check the table definition to see what constraints have been declared and check your data to determine what constraint is being violated.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

skull52IT director

Author

Commented:
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.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
Or just CAST it as date:

SET D.CustUpdDate = CAST(GETDATE()  AS date)
skull52IT director

Author

Commented:
Scott, Yeah... that would be less complicated, thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial