September 12, 2008 at 8:02 am
In the trigger below, I am getting an error when deleting > 1 row at a time.
I understand that the deleted table will contain all rows deleted in one statement but
how do I need to handle that in this trigger? I'm not getting it.
CREATE TRIGGER [tr_ptChemoMedicareBilling_StoreCharges_del]
ON [dbo].[ptChemoMedicareBilling]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON
DECLARE @MRN varchar(25)
DECLARE @dos datetime
DECLARE @ChargeSourceID int
SET @MRN = (SELECT MRN FROM deleted)
SET @dos = (SELECT DateOfService FROM ptChemoOrders o
WHERE o.OrderID = (SELECT OrderID FROM deleted)
)
SET @ChargeSourceID = (SELECT ID FROM deleted)
DELETE FROM ptEncounterDetailsCharges
WHERE MRN = @MRN
AND DOS = @dos
AND ChargeSourceID = @ChargeSourceID
SET NOCOUNT OFF
END
GO
September 12, 2008 at 8:13 am
Here are 2 options:
[font="Courier New"]CREATE TRIGGER [tr_ptChemoMedicareBilling_StoreCharges_del]
ON [dbo].[ptChemoMedicareBilling]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON
DELETE P
FROM
ptEncounterDetailsCharges P JOIN
deleted D ON
P.MRN = D.MRN AND
P.DOS = D.DOS AND
P.CHARgeSourceID = D.CHARgeSourceID
OR
DELETE
FROM
ptEncounterDetailsCharges
WHERE
EXISTS(SELECT * FROM deleted D WHERE MRN = D.MRN AND
DOS = D.DOS AND
CHARgeSourceID = D.CHARgeSourceID)
SET NOCOUNT OFF
END
GO[/font]
I recommend against using variables in triggers for this very problem. You could implement a RBAR (cursor or loop) solution, but that will kill your performance.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 12, 2008 at 8:28 am
Thanks, Jack, but the DOS comes from the ptChemoOrders table not from the
ptChemoMedicalBilling table that the trigger is on. The ptChemoMedicalBilling record is based on the OrderID in ptChemoOrders. How would I do that join?
September 12, 2008 at 8:41 am
Here are the relations:
Table ptChemoOrders:
-------------------------------
OrderID -- PK
DateOfService
MRN
Table ptChemoMedicareBilling:
-------------------------------
ID -- PK
OrderID -- pointer to ptChemoOrders.OrderID
Table ptEncounterDetailsCharges:
-------------------------------
DOS -- pointer to ptChemoOrders.DateOfService
MRN -- pointer to ptChemoOrders.MRN
ChargeSourceID -- pointer to ptChemoMedicareBilling.ID
September 12, 2008 at 8:43 am
MrBaseball34 (9/12/2008)
Thanks, Jack, but the DOS comes from the ptChemoOrders table not from theptChemoMedicalBilling table that the trigger is on. The ptChemoMedicalBilling record is based on the OrderID in ptChemoOrders. How would I do that join?
Oops, that's what happens when you skim. I think these changes will help:
[font="Courier New"]CREATE TRIGGER [tr_ptChemoMedicareBilling_StoreCharges_del]
ON [dbo].[ptChemoMedicareBilling]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON
DELETE P
FROM
ptEncounterDetailsCharges P JOIN
deleted D ON
P.MRN = D.MRN AND
P.CHARgeSourceID = D.CHARgeSourceID JOIN
ptChemoOrders CO ON
D.OrderId = CO.OrderId AND
P.DOS = CO.DateOfService
OR
DELETE
FROM
ptEncounterDetailsCharges
WHERE
EXISTS(SELECT * FROM deleted D JOIN ptChemoOrders CO ON
D.OrderId = CO.OrderId WHERE MRN = D.MRN AND
CO.DOS = D.DOS AND
CHARgeSourceID = D.CHARgeSourceID)
SET NOCOUNT OFF
END
GO[/font]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 12, 2008 at 8:49 am
I believe that may work for me. let me test it right quick.
September 12, 2008 at 9:14 am
Yepper, that did it. Thanks a million.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply