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 March 25th, 2003, 11:41 AM
wels wels is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Mi, USA
Posts: 1 wels User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to wels Send a message via Yahoo to wels
Question how to get your trigger to catch the recordID when a specific record is updated

how can you make your trigger retrive a valuw from the record that fired the triger

I am trying to create a triger ( insert, update ) that will excute some stored procedures but the stored procedures need the record ID to function correctly
( I managed to to get the ID (generated outomaticaly when insert) by
SELECT MAX(recordID) From tableName
the problem is that when I update any record it always return the same ID regardless of what record is actually been updated and always return the ID of the last record inserted in the table instead

Reply With Quote
  #2  
Old March 26th, 2003, 07:23 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Have a read up on "SELECT @@IDENTITY" - it returns the last inserted ID....

ps - never use "select max()" to get the last inserted ID - you'll run into problems when you get other people using the db... e.g. if someone inserts a record before you make that call, you'll get the ID of their record, and not yours!

Reply With Quote
  #3  
Old March 27th, 2003, 08:34 AM
sqlboy sqlboy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Memphis
Posts: 10 sqlboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
As stumpy mention, used select @@ IDENTITY, it will sort out your problem. As far as Select Max() is concerned then it will put you in more trouble under load.

SELECT MAX(ID) as new_id FROM <tblname> and then Insert into <tblname>(field1, field2.....) values(value1, value2...)

It doesn't work. Yes, read that again. The first insert would lock whatever page the insert occurred on. 99% of the time, this would make the SELECT MAX() successful.

However, under load, another process could do an insert that ended up on a new page and cause the subsequent MAX() to be incorrect for the first process. SQL 7 does row locking. So, the SELECT MAX() almost never works.

You can try it for yourself. If you're running on SQL7, try the following test. Create two files. Each file should insert into the same table as above. However, the first file should have a long (like 60 second) loop before the SELECT MAX() (but still in the transaction). Then, run the two files in two instances of a browser. output the ID from the SELECT MAX() in each file's query and you'll see (to your horror, I'm afraid) that they are the same.

sqlboy

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > how to get your trigger to catch the recordID when a specific record is updated


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