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 392,009 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 4,232 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:

Trigger updated values not as expected

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  
All times are GMT -4. The time now is 10:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC