Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger Problems

Status
Not open for further replies.

storm75m

Programmer
Apr 18, 2001
81
0
0
US
I have a table named AcctTypes with these fields defined:

AcctID
Name
Location
Phone
ModUsr
ModDate

Any time that the data is changed in any field in this table, I would like to update the ModUsr to be the current username, and ModDate to be today's date/time. I've never created a trigger before, and my first attempt at this is continuously giving me Write Conflict errors. Here is what I had so far:

CREATE TRIGGER [TR_AcctTypes_Update] ON [dbo].[AcctTypes]
AFTER UPDATE
AS
set nocount on
UPDATE AcctTypes
SET ModUsr = SUSER_SNAME(), ModDate = GETDATE()

This is giving me an error almost every time I try to update a record, and no one seems to know what's wrong... (I've posted this problem before, and googled this to death) Any help would be much appreciated...
 
Code:
CREATE TRIGGER [TR_AcctTypes_Update] ON [dbo].[AcctTypes] 
AFTER UPDATE 
AS
UPDATE    AcctTypes
SET ModUsr = SUSER_SNAME(), 
    ModDate = GETDATE()
FROM AcctTypes INNER JOIN inserted
ON AcctTypes.PKField = inserted.PKField

James Goodman MCSE, MCDBA
 
Thanks, I'll give this a shot. What if my Primary key is defined on two fields?
 
I used PKField just for illustrative purposes.

It is a standard SQL statement, & you just need to modify as appropriate in order to only modify the records you want to modify.

James Goodman MCSE, MCDBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top