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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server 2000 trigger problem

Status
Not open for further replies.

ChrisQuick

Programmer
Oct 4, 1999
144
US
I have a collection of tables used for an online discussion forum.

We have 3 tables:
SupportForums - List of Forums.
SupportForumTopics - List of Messages, by forum.
SupportForumsReplies - List of Message replies by Forum & Topic.

I have the following trigger on the table SupportForumsReplies:

CREATE TRIGGER [AddToTopicReplyTotal] ON dbo.SupportForumsReplies
FOR INSERT
AS

/* Update the count for the total number of messages in the forum */
Update SupportForums Set SupportMessageTotal = ISNULL(SupportMessageTotal, 0) + 1, SupportLastMessageDate = getDate()
where SupportForum_id = (Select SupportForum_Id from inserted)

/* Update the count for the total number of replies to this individual thread */
Update SupportForumTopics Set SupportMessageResponseTotal = ISNULL(SupportMessageResponseTotal, 0) + 1
where SupportForumTopics.SupportForumsTopic_id = (Select SupportForumsTopic_id from inserted)


/*Update the SupportForumTopics table for this record to show the LastMessageDate*/
Update SupportForumTopics Set SupportLastMessageDate = getDate()
where SupportForumTopics.SupportForumsTopic_id = (Select SupportForumsTopic_id from inserted)


/*Update this table for this record to show the LastMessageDate*/
Update SupportForumsReplies Set SupportForumSubmittedDate = getDate()
where SupportForumReply_id = (Select SupportForumReply_id from inserted)


The problem is the update statements are effecting EVERY record in the SupportForumTopics table even though our where clause should be restricting the update statement to the SupportForumTopics.SupportForumsTopic_id referenced.

Any thoughts??? cquick@geotg.com
Geographic Information System (GIS), ASP, some Oracle
 
Hi Chris,
Try to do like following:
CREATE TRIGGER [AddToTopicReplyTotal] ON dbo.SupportForumsReplies
FOR INSERT
AS
/* Update the count for the total number of messages in the forum */
Update SupportForums Set SupportMessageTotal = ISNULL(SupportMessageTotal, 0) + 1, SupportLastMessageDate = getDate()
FROM SupportForums a, inserted i
where a.SupportForum_id = i.SupportForum_Id

/* Update the count for the total number of replies to this individual thread */
Update SupportForumTopics Set SupportMessageResponseTotal = ISNULL(SupportMessageResponseTotal, 0) + 1
FROM SupportForumTopics a, inserted i
where a.SupportForumsTopic_id = i.SupportForumsTopic_id

/*Update the SupportForumTopics table for this record to show the LastMessageDate*/
Update SupportForumTopics Set SupportLastMessageDate = getDate()
FROM SupportForumTopics a, inserted i
where a.SupportForumsTopic_id = i.SupportForumsTopic_id

/*Update this table for this record to show the LastMessageDate*/
Update SupportForumsReplies Set SupportForumSubmittedDate = getDate()
FROM SupportForumsReplies a, inserted i
where a.SupportForumReply_id = i.SupportForumReply_id

 
Thank you!! That fixed it.
cquick@geotg.com
Geographic Information System (GIS), ASP, some Oracle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top