We help IT Professionals succeed at work.

Error when creating an UPDATE Trigger in SQL

159 Views
Last Modified: 2017-03-10
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

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
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions