I have two stored procedures...
Create a stored procedure to calculate the price of a charter and update the row in the Charter table. The stored procedure takes a @CharTrip (numeric) parameter. This parameter is the primary key to the Charter table.
The price of a Charter is determined by:
The mileage charge for the Model for the Aircraft used in the charter.
This is multiplied by the miles flown for the charter.
There is a 79 cent per gallon fuel charge for every gallon used by the charter flight
There is a wait charge of $112.50 for each hour that the charter is on the ground waiting.
The sum of these charges is the price for the charter.
The per gallon surcharge and the hour wait charge should be prominent in the stored procedure so that they can be easily changed by a T-SQL analyst when the business conditions change.
CREATE PROCEDURE spCharterPrice(@CharTrip NUMERIC)
@FuelCharge money = 0.79 -- VARIABLE PER GALLON FUEL RATE
, @WaitCharge money = 112.50 -- VARIABLE PER HOUR WAIT RATE
, CAST(ROUND(SUM (([MOD_CHG_MILE]*[CHAR_DISTANCE]) + ([CHAR_FUEL_GALLONS]* @FuelCharge) + ([CHAR_HOURS_WAIT]* @WaitCharge)),2) AS decimal(6,2)) AS Price
FROM AIRCRAFT A Inner Join CHARTER C on A.AC_NUMBER = C.AC_NUMBER
Inner Join MODEL M on A.MOD_CODE = M.MOD_CODE
WHERE C.CHAR_TRIP = @CharTrip
GROUP BY C.CHAR_TRIP, C.AC_NUMBER
Create a stored procedure to calculate the history of an aircraft and update the row in the Aircraft table. The stored procedure takes an @AC_NUMBER (Char (5)) parameter. This history includes:
The number of trips
The distance flown
The hours flown
Number of gallons of fuel used
The number of quarts of oil used
CREATE PROCEDURE spAircraftHistory
SET TripCount = foo.Trips
, DistanceFlown = foo.Distance
, HoursFlown = foo.Hours
, FuelUsed = foo.Fuel
, OilUsed = foo.Oil
FROM AIRCRAFT A
INNER JOIN (
, Count(C.CHAR_TRIP) AS Trips
, Sum(C.CHAR_DISTANCE) AS Distance
, Sum(C.CHAR_HOURS_FLOWN) AS Hours
, Sum(C.CHAR_FUEL_GALLONS) AS Fuel
, Sum(C.CHAR_OIL_QTS) As Oil
FROM CHARTER C
GROUP BY AC_NUMBER
) AS foo
ON A.AC_NUMBER = foo.AC_NUMBER
Now I want to create a Trigger that calls them for the following scenarios...
Write an insert, delete and update trigger named trgCharterUpdate for the Charter Table. These will update the Price column on the Charter table and the Number of Trips, Distance Flown, Hours Flown, Fuel and Oil Used on the Aircraft table. Be aware that a charter can change the aircraft which will affect two aircrafts--there will be a trigger fired for each.
Formatted text for readbility and code snipppets