User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 373,936 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,612 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 671 | Replies: 6
Reply
Join Date: Dec 2007
Posts: 4
Reputation: SyCo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SyCo SyCo is offline Offline
Newbie Poster

Question Trigger updated values not as expected

  #1  
Dec 11th, 2007
Hi,
I have a simple SQL query
  1. UPDATE testcourselog SET grade='a' WHERE statusid=4
It updates 114 records

I am trying to write a trigger that will change a flag to 1 for all record updated by the query.

  1. USE [online]
  2. GO
  3. /****** Object: Trigger [dbo].[gl_courselog_iscounted_update] Script Date: 12/10/2007 16:49:32 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author: <Author,,Name>
  10. -- Create date: <Create Date,,>
  11. -- Description: <Description,,>
  12. -- =============================================
  13. ALTER TRIGGER [dbo].[gl_courselog_iscounted_update]
  14. ON [dbo].[testcourselog]
  15. AFTER INSERT,UPDATE
  16. AS
  17. IF UPDATE(grade) AND (SELECT count(*) FROM inserted) > 0
  18. BEGIN
  19.  
  20. IF @@ROWCOUNT = 1
  21. BEGIN
  22.  
  23. SET NOCOUNT ON;
  24. UPDATE testcourselog SET iscounted=1 WHERE userid=(SELECT userid FROM inserted)
  25.  
  26. END
  27. ELSE
  28. BEGIN
  29.  
  30. UPDATE testcourselog SET iscounted=1
  31. FROM testcourselog t JOIN deleted d
  32. ON t.userid = d.userid
  33. --WHERE d.statusid=4 --I think this shouldn't be required
  34.  
  35.  
  36. END
  37. END

THe problem is the trigger is updating 214 records and they appear to have no relation to the join.

The 3 columns below are statusid, grade and iscounted

8	NULL      	False
8	NULL      	False
7	NULL      	False
8	NULL      	False
8	NULL      	True
4	a           	True
4	a           	True
2	NULL	True
2      	NULL      	True
2      	NULL      	False
8      	NULL      	False
6      	NULL      	True
8      	NULL      	False
6      	NULL      	False
8      	NULL      	True
4      	a           	True
8      	NULL      	True

You can see the grade updated correctly then the trigger updated extra records. Any idea why?
Last edited by SyCo : Dec 11th, 2007 at 6:03 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 4
Reputation: SyCo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SyCo SyCo is offline Offline
Newbie Poster

Re: Trigger updated values not as expected

  #2  
Dec 11th, 2007
Doh, my first post and it turned out I messed up!

I was joining on a non unique value. Hence the confused results.

It's still be nice if you say hi
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 743
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 3
Solved Threads: 53
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: Trigger updated values not as expected

  #3  
Dec 12th, 2007
Hi
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Dec 2004
Posts: 402
Reputation: Sulley's Boo will become famous soon enough Sulley's Boo will become famous soon enough 
Rep Power: 5
Solved Threads: 9
Sulley's Boo's Avatar
Sulley's Boo Sulley's Boo is offline Offline
Posting Pro in Training

Re: Trigger updated values not as expected

  #4  
Dec 21st, 2007
Hiii
Reply With Quote  
Join Date: Dec 2007
Posts: 4
Reputation: SyCo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SyCo SyCo is offline Offline
Newbie Poster

Re: Trigger updated values not as expected

  #5  
Apr 9th, 2008
See, that was nice.
Reply With Quote  
Join Date: Dec 2004
Posts: 402
Reputation: Sulley's Boo will become famous soon enough Sulley's Boo will become famous soon enough 
Rep Power: 5
Solved Threads: 9
Sulley's Boo's Avatar
Sulley's Boo Sulley's Boo is offline Offline
Posting Pro in Training

Re: Trigger updated values not as expected

  #6  
Apr 9th, 2008
^

welcome back
Reply With Quote  
Join Date: Dec 2007
Posts: 4
Reputation: SyCo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SyCo SyCo is offline Offline
Newbie Poster

Re: Trigger updated values not as expected

  #7  
Apr 9th, 2008
Hi, I got my kick in the butt email. My bosds sprang for an expex account, but honestly it's not the greatest value.

I guess I'll try to be here more often, see you around!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 6:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC