R
Rhonda Fischer
Hello,
I am trying to design a query that upon record INSERT
checks the number of pallets ordered
and ensures that no record has more than 26 pallets, as
this is the maximum that
will fit onto a trailer. Some how dividing 26 into the
number of pallets copying all lines with less
than 26 pallets into a new table and copying records with
over 26 pallets in quantities of 26 and
then the final last record of less than 26.
Before Trigger:
tblOrder
DeliveryID CollectionID TempID OrderDate Loads
2 3 3 30/03/2004 45
2 4 3 30/03/2004 27
2 5 3 30/03/2004 13
After Trigger: copied split entries of max 26 pallets to a
new table
tblSplitOrder
Id DeliveryID CollectionID TempID OrderDate Loads
1 2 3 3 30/03/2004 26
2 2 3 3 30/03/2004 19
3 2 4 3 30/03/2004 26
4 2 4 3 30/03/2004 1
5 2 5 3 30/03/2004 13
I tried the follow query but have not got very far -
struggling. Hope you can help.
Thank you very much for any ideas you might have.
Best Regards
Rhonda
I tried to use the follow as a framework but it doesn't
work in it's current form and I haven't got very far
redesigning it.
===========================================================
==================
CREATE TRIGGER trgLoadExceedTrlCapacity ON [tblOrders]
FOR INSERT, UPDATE, DELETE
AS
/* The purpose of this trigger is to ensure that no
entries have in excess of 26 pallet loads */
/* The Supermarket may order more than 26
loads of produce but the Trailer can only manage 26 loads
*/
/* so extra lines are inserted into the new
table tblSplitOrders with a maximum of 26 loads */
/* and an extra line for the remaining pallets
*/
DECLARE @NumOfLoads int,
@LoadsRemainder int,
@CountSplit int,
@Cnt int
/* Number of Loads from tblOrders saved to
variable */
@NumOfLoads = NumOfLoads
@LoadsRemainder = 0
@CountSplit = 0
@Cnt = 0
/* Determine if there are more than 26 loads */
@LoadsRemainder = @NumOfLoads / 26
if (@LoadsRemainder >= 1) then
while (@LoadsRemainder >= 1)
@NumOfLoads = @NumOfLoads - 26
@CountSplit = @CountSplit + 1
@LoadsRemainder = @NumOfLoads / 26
end
for @Cnt = 0 to (@CountSplit - 1)
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate, 26)
FROM tblOrders
end
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate,
@NumOfLoads) FROM tblOrders
else
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate,
@NumOfLoads) FROM tblOrders
end if
END
===========================================================
===================
I am trying to design a query that upon record INSERT
checks the number of pallets ordered
and ensures that no record has more than 26 pallets, as
this is the maximum that
will fit onto a trailer. Some how dividing 26 into the
number of pallets copying all lines with less
than 26 pallets into a new table and copying records with
over 26 pallets in quantities of 26 and
then the final last record of less than 26.
Before Trigger:
tblOrder
DeliveryID CollectionID TempID OrderDate Loads
2 3 3 30/03/2004 45
2 4 3 30/03/2004 27
2 5 3 30/03/2004 13
After Trigger: copied split entries of max 26 pallets to a
new table
tblSplitOrder
Id DeliveryID CollectionID TempID OrderDate Loads
1 2 3 3 30/03/2004 26
2 2 3 3 30/03/2004 19
3 2 4 3 30/03/2004 26
4 2 4 3 30/03/2004 1
5 2 5 3 30/03/2004 13
I tried the follow query but have not got very far -
struggling. Hope you can help.
Thank you very much for any ideas you might have.
Best Regards
Rhonda
I tried to use the follow as a framework but it doesn't
work in it's current form and I haven't got very far
redesigning it.
===========================================================
==================
CREATE TRIGGER trgLoadExceedTrlCapacity ON [tblOrders]
FOR INSERT, UPDATE, DELETE
AS
/* The purpose of this trigger is to ensure that no
entries have in excess of 26 pallet loads */
/* The Supermarket may order more than 26
loads of produce but the Trailer can only manage 26 loads
*/
/* so extra lines are inserted into the new
table tblSplitOrders with a maximum of 26 loads */
/* and an extra line for the remaining pallets
*/
DECLARE @NumOfLoads int,
@LoadsRemainder int,
@CountSplit int,
@Cnt int
/* Number of Loads from tblOrders saved to
variable */
@NumOfLoads = NumOfLoads
@LoadsRemainder = 0
@CountSplit = 0
@Cnt = 0
/* Determine if there are more than 26 loads */
@LoadsRemainder = @NumOfLoads / 26
if (@LoadsRemainder >= 1) then
while (@LoadsRemainder >= 1)
@NumOfLoads = @NumOfLoads - 26
@CountSplit = @CountSplit + 1
@LoadsRemainder = @NumOfLoads / 26
end
for @Cnt = 0 to (@CountSplit - 1)
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate, 26)
FROM tblOrders
end
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate,
@NumOfLoads) FROM tblOrders
else
INSERT INTO tblSplitOrders (DeliveryID, CollectionID,
TempID, OrderDate, NumOfLoads)
SELECT (DeliveryID, CollectionID, TempID, OrderDate,
@NumOfLoads) FROM tblOrders
end if
END
===========================================================
===================