WHOA THERE!

The biggest misconception that people have with the @@identity global
variable is that it will give you that last identity inserted for a
particular table. It actually gives you the last identity inserted for ANY
identity column in the database. In a high impact environment this could
give you the wrong value, especially if you are using it in a trigger.

I STRONGLY recommend using the SCOPE_IDENTITY(), especially in triggers.
This will give you the last value inserted into the table when using it in a
trigger since it gives you the last identity value in the scope.

Please check out the BOL for SQL Server and you will see a comparison
between SCOPE_IDENTITY() and @@IDENTITY.




-----Original Message-----
From: Robert Bailey [mailto:[EMAIL PROTECTED] 
Sent: Monday, January 10, 2005 7:14 PM
To: CF-Talk
Subject: SQL Triggers

A little rusty at the triggers I see, but here is what I got:

CREATE TRIGGER dbo.MyTest ON dbo.UserTime AFTER INSERT      AS

DECLARE @UserTimeProjID Int , @OrderProjID Int, @InternalCostAdd
Int,@ActualHours Int

Select @UserTimeProjID, @OrderProjID, @InternalCostAdd,@ActualHours 

(

SELECT    UT.ProjectID, Orders.ProjectID, UR.UserRate * UT.ActualHours ,
UT.ActualHours
FROM         Orders INNER JOIN
                      UserTime UT ON Orders.OrderID = UT.OrderID INNER
JOIN
                      UserRates UR ON UT.UserID = UR.UserID AND
UT.UserTimeID = @@IDENTITY

)

UPDATE ProjectCosts
SET InternalCost = ProjectCosts.InternalCost + @InternalCostAdd,
ActualHours = ProjectCosts.ActualHours + @ActualHours 
WHERE ProjectID = @UserTimeProjID
OR ProjectID = @OrderProjID
go


I am not sure if the @@identity works for the inserted row and if my
select statement is correct. Any pointers?

Robert Bailey
Software Engineer
813-230-9967 \\ mobile
[EMAIL PROTECTED]
www.recruitmax.com




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Get the mailserver that powers this list at 
http://www.houseoffusion.com/banners/view.cfm?bannerid=17

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:189870
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to