Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 8th, 2003, 06:25 PM
matrix matrix is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 matrix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post updating field using trigger in Ms sql2000

hi guys,

need help with triggers.

i have 2 tbls. tblContact & tblMemo (1-m) resp..

tblContact [PK contId(int 4), Name, MemoCount(int 4)]
tblMemo[pk memoId (int 4), {FK} contId(int 4), memo (varchar 100)]

I need to Write a trigger to automatically update the MemoCount field in the tblContact whenever a memo from tblMemo is inserted, updated or deleted. Assuming that the memoCount field might not have the correct value so have to take that into account when writting the trigger.

any help will be really appreciated.
am new to sql server..


thanks
mat..

Reply With Quote
  #2  
Old August 5th, 2003, 02:45 PM
rdoekes rdoekes is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Strasbourg, France
Posts: 181 rdoekes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 7
Send a message via AIM to rdoekes Send a message via Yahoo to rdoekes
Try this. Loosely tested.
Code:
CREATE TRIGGER UpdateMemoCount ON [dbo].[tblMemo] 
FOR INSERT, UPDATE, DELETE 
AS

DECLARE @ContactID int
DECLARE @MemoCount int
IF EXISTS(SELECT 1 FROM deleted)
	SELECT @contactID = contactID FROM deleted
ELSE	
	SELECT @contactID = contactID FROM inserted


IF Exists(SELECT 1 FROM tblMemo WHERE contactID = @contactid)
	SELECT @memoCount = COUNT(*) FROM tblMemo WHERE contactId = @contactid
ELSE
	SELECT @memoCount = 0

UPDATE tblContact SET memoCount = @memoCount WHERE contactID = @contactid


There must be better and faster ways of doing this, but you get the picture.
__________________
- Rogier Doekes

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > updating field using trigger in Ms sql2000


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT