davidgoulet
Technical User
I am having a performance problem using a cursor. Looking for an alternative method, that I can understand.
I have two tables: 1. On order header table (opheadm) and
and order detail table (opdetm). I am trying to set the delivery method on the order header tables, based on the total weight of the order, which unfortunately is held against each detail line.
The trigger works O.K. - but the performance is very slow and slowing down.
Here is my trigger:
CREATE TRIGGER [weight_update] ON [scheme].[opdetm]
FOR INSERT, UPDATE
AS
IF (UPDATE(weight))
DECLARE @order_no char (10)
DECLARE @carrier char (10)
DECLARE @transaction_anals3 char (10)
DECLARE @delivery_method char (10)
DECLARE @weight FLOAT
DECLARE e1 cursor for
select h.order_no, h.carrier_code, h.delivery_method, h.transaction_anals3, sum(d.order_qty * d.weight)
from scheme.opheadm h, scheme.opdetm d, inserted i
where h.order_no = i.order_no
and d.order_no = i.order_no
group by h.order_no, h.carrier_code, h.delivery_method, h.transaction_anals3
OPEN e1
FETCH NEXT from e1 into @order_no, @carrier, @delivery_method, @transaction_anals3, @weight
WHILE @@FETCH_STATUS = 0
BEGIN
/* need to add delivery_method based on weight and other factors*/
IF @order_no like '%/%' set @delivery_method = 'PPD'
else
IF @weight > 750 and @delivery_method = ' ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPD'
else
IF @weight > 750 and @delivery_method = 'PDA ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPD'
else
IF @weight < 750 and @delivery_method = ' ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PDA'
else
IF @weight < 750 and @delivery_method = 'PPD ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPA'
else
IF @transaction_anals3 = 'SAMPLE' set @delivery_method = 'PNA'
update scheme.opheadm
set delivery_method = @delivery_method
where scheme.opheadm.order_no = @order_no
FETCH NEXT from e1 into @order_no, @carrier, @delivery_method, @transaction_anals3, @weight
END
CLOSE e1
DEALLOCATE e1
I have two tables: 1. On order header table (opheadm) and
and order detail table (opdetm). I am trying to set the delivery method on the order header tables, based on the total weight of the order, which unfortunately is held against each detail line.
The trigger works O.K. - but the performance is very slow and slowing down.
Here is my trigger:
CREATE TRIGGER [weight_update] ON [scheme].[opdetm]
FOR INSERT, UPDATE
AS
IF (UPDATE(weight))
DECLARE @order_no char (10)
DECLARE @carrier char (10)
DECLARE @transaction_anals3 char (10)
DECLARE @delivery_method char (10)
DECLARE @weight FLOAT
DECLARE e1 cursor for
select h.order_no, h.carrier_code, h.delivery_method, h.transaction_anals3, sum(d.order_qty * d.weight)
from scheme.opheadm h, scheme.opdetm d, inserted i
where h.order_no = i.order_no
and d.order_no = i.order_no
group by h.order_no, h.carrier_code, h.delivery_method, h.transaction_anals3
OPEN e1
FETCH NEXT from e1 into @order_no, @carrier, @delivery_method, @transaction_anals3, @weight
WHILE @@FETCH_STATUS = 0
BEGIN
/* need to add delivery_method based on weight and other factors*/
IF @order_no like '%/%' set @delivery_method = 'PPD'
else
IF @weight > 750 and @delivery_method = ' ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPD'
else
IF @weight > 750 and @delivery_method = 'PDA ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPD'
else
IF @weight < 750 and @delivery_method = ' ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PDA'
else
IF @weight < 750 and @delivery_method = 'PPD ' and @transaction_anals3 = 'SALE' set @delivery_method = 'PPA'
else
IF @transaction_anals3 = 'SAMPLE' set @delivery_method = 'PNA'
update scheme.opheadm
set delivery_method = @delivery_method
where scheme.opheadm.order_no = @order_no
FETCH NEXT from e1 into @order_no, @carrier, @delivery_method, @transaction_anals3, @weight
END
CLOSE e1
DEALLOCATE e1