June 7, 2005 at 10:28 am
This is probably really simple but I am really knew at this type of sql work. I created a custom table and two triggers that will populate that table. I've gotten both of them to fire and fill in data however the 2nd of the two (The Update Trigger) is updating all the records in the table instead of just the one it is suppose to. If i need to add some more background let me know. Thank you in advance for any help.
Ryan
--on Access Table when record is inserted
--to populate the custom table "LOGON" so that frequency and
--duration of users time in the DB can be tracked
If EXISTS (Select name from sysobjects where name = 'LogOn_Insert'
and type = 'TR')
on ACCESS
For
INSERT
as INSERT LogOn
(AccessDate, AccessNbr, CompanyID, DatabaseName, ScrnNbr,
SessionCntr, UserId)
SessionCntr, UserId from inserted WHERE SCRNNBR = 'MENU '
--on Access Table when record is deleted
If EXISTS (Select name from sysobjects where name = 'LogOn_Update'
and type = 'TR')
GO
on ACCESS
For
DELETE
as UPDATE LogOn SET LogoffDate = GETDATE()
and logon.userid = deleted.userid and logon.accessnbr = deleted.accessnbr
Ryan Grant
Financial Systems Consultant
Elypsis, Inc
ryan@elypsis.com
June 7, 2005 at 2:26 pm
Your trigger updates all rows becuase your update statement has NO where clause. The Select is just hanging out there attached to nothing. You might be looking for something like this.
** This is wrong - see Remi's post below **
CREATE TRIGGER LogOn_Update
on ACCESS
For
DELETE
as
logon.userid = deleted.userid and
logon.accessnbr = deleted.accessnbr
Consider renaming this to LogOn_Delete as it is designed to fire when rows are deleted from Access.
June 7, 2005 at 2:30 pm
To finish Ron's thaught :
CREATE TRIGGER LogOn_Delete
on ACCESS
For
DELETE
as
SET NOCOUNT ON
UPDATE L
SET LogoffDate = GETDATE()
FROM LogOn L inner join Deleted D on L.scrnnbr = D.scrnnbr and L.userid = D.userid and L.accessnbr = D.accessnbr
June 7, 2005 at 2:40 pm
Show off
June 7, 2005 at 2:45 pm
I'm trying to stop... but I like the users to have the right answer.. sorry about that .
June 7, 2005 at 2:58 pm
Was there something wrong with my post??
I'm always open to suggestions for improvement.
June 7, 2005 at 3:02 pm
hmm.. I don't see any reference to the deleted table in here :
UPDATE LogOn
SET LogoffDate = GETDATE()
Where
logon.scrnnbr = deleted.scrnnbr and
June 7, 2005 at 3:07 pm
So I'm still a show off??
June 7, 2005 at 3:07 pm
duh! Thanks
June 7, 2005 at 3:19 pm
Well...just a little...but that's ok.
I'd just looked at my triggers and at least I didn't make that mistake there!
June 7, 2005 at 5:04 pm
I don't think they would compile if the table name is correct. But I can't test from here, maybe tomorrow.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply