Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Am I duplicating using IF and WHERE in a trigger? 1

Status
Not open for further replies.

Vulton

IS-IT--Management
Sep 3, 2005
22
US
Here is the SQL trigger. It seems to me that checking in the inserted table in the IF statement means I don't need it in the WHERE clause. Any help simplifying this would be appreciated.

Code:
CREATE TRIGGER TriggerInstallationComplete ON [mpadmin].[TblLotVisitsReason] 
FOR INSERT
AS
IF EXISTS (SELECT inserted.LVR_Reason From inserted WHERE inserted.LVR_Reason = '14')
BEGIN
UPDATE dbo.TblLotEvents 
SET dbo.TblLotEvents.LE_OrderParts = inserted.LVR_TimeStamp
FROM dbo.TblLotEvents INNER JOIN (mpadmin.TblLotVisits INNER JOIN inserted
ON mpadmin.TblLotVisits.LV_ID = inserted.LVR_ID)
ON dbo.TblLotEvents.LE_JHL = mpadmin.TblLotVisits.LV_PJHL
WHERE (((mpadmin.TblLotVisits.LV_PJHL)=dbo.TblLotEvents.LE_JHL) AND ((inserted.LVR_Reason)="14"));
END

IF EXISTS (SELECT inserted.LVR_Reason From inserted WHERE inserted.LVR_Reason = '24')
BEGIN
UPDATE dbo.TblLotEvents 
SET dbo.TblLotEvents.LE_TurnOverlot = inserted.LVR_TimeStamp
FROM dbo.TblLotEvents INNER JOIN (mpadmin.TblLotVisits INNER JOIN inserted
ON mpadmin.TblLotVisits.LV_ID = inserted.LVR_ID)
ON dbo.TblLotEvents.LE_JHL = mpadmin.TblLotVisits.LV_PJHL
WHERE (((mpadmin.TblLotVisits.LV_PJHL)=dbo.TblLotEvents.LE_JHL) AND ((inserted.LVR_Reason)="24"));
END

Thanks,
Mark
 
this should do it
Code:
CREATE TRIGGER TriggerInstallationComplete ON [mpadmin].[TblLotVisitsReason] 
FOR INSERT

UPDATE dbo.TblLotEvents 
SET dbo.TblLotEvents.LE_OrderParts = inserted.LVR_TimeStamp
FROM dbo.TblLotEvents 
INNER JOIN (mpadmin.TblLotVisits 
INNER JOIN inserted
ON mpadmin.TblLotVisits.LV_ID = inserted.LVR_ID)
ON dbo.TblLotEvents.LE_JHL = mpadmin.TblLotVisits.LV_PJHL
AND inserted.LVR_Reason in ("14","24");
 
Wow fast response thanks pwise,
Only thing is the SET is seting different fields based on the inserted.LVR_Reason data.

14 = SET dbo.TblLotEvents.LE_OrderParts = inserted.LVR_TimeStamp

24 = SET dbo.TblLotEvents.LE_TurnOverlot = inserted.LVR_TimeStamp

Thanks,
Mark
 
try this

Code:
UPDATE dbo.TblLotEvents 
SET 
dbo.TblLotEvents.LE_OrderParts = case inserted.LVR_Reason when "14" then inserted.LVR_TimeStamp else dbo.TblLotEvents.LE_OrderParts end ,--14
dbo.TblLotEvents.LE_TurnOverlot = case inserted.LVR_Reason when "24" then inserted.LVR_TimeStamp else dbo.TblLotEvents.LE_TurnOverlot end --24
FROM dbo.TblLotEvents 
INNER JOIN (mpadmin.TblLotVisits 
INNER JOIN inserted
ON mpadmin.TblLotVisits.LV_ID = inserted.LVR_ID)
ON dbo.TblLotEvents.LE_JHL = mpadmin.TblLotVisits.LV_PJHL
AND inserted.LVR_Reason in ("14","24");
 
Fantastic Pwise,
Thanks very much for the help. It works like a charm and is easy to read and add more case's to.

Thanks again,
Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top