Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Trigger ... How do I get a "curent" row value???

Author  Topic 

FastCougar
Starting Member

3 Posts

Posted - 2004-12-01 : 08:51:55
I realize the triggers fire in batches and fully understand the way that the virtual tables of INSERTED and DELETED are used. However, I need to find a value within the "current" row that is being updated/inserted.

I have a table called DispatchRatedTickets layed out like so:

ratingID | ticketID | chargeID | chargeQuantity | chargeAmount | chargeCost | checkNum
21 | 698 | 0 | 457 | 54.84 | 23.58 | NULL
22 | 712 | 10 | 775 | 65.24 | 36.28 | NULL

If I change the second row's chargeQuantity value from "775" to "900", I want to get the "current" row's ratingID. My desired results are to set @thisRatingID=22. Below is my trigger and as you can see, everything is hinged on me finding the "current" row's ratingID.
CREATE TRIGGER trg_update_ratedtickets_cost_charge 
ON dbo.DispatchRatedTickets
FOR UPDATE, INSERT
AS
BEGIN
DECLARE @thisRatingID as int
DECLARE @thisChargeQuantity as int
DECLARE @thisTicketID as int
DECLARE @thisChargeID as int
DECLARE @thisManifestID as int
DECLARE @thisDriverID as int
DECLARE @thisCompanyID as int
DECLARE @thisPostalCode as varchar
DECLARE @thisTotalWeight as int
DECLARE @thisChargeRate as float
DECLARE @thisChargeMin as int
DECLARE @thisFreightCharge as money
DECLARE @thisFreightCost as money
DECLARE @newAmount as money
DECLARE @newCost as money
DECLARE @newchargeAmount as money
DECLARE @newChargeCost as money

SET @thisRatingID = (SELECT ratingID FROM DispatchRatedTickets WHERE ratingID IN (SELECT ratingID FROM inserted))
SET @thisChargeQuantity = (SELECT chargeQuantity FROM DispatchRatedTickets WHERE ratingID = @thisRatingID)
SET @thisTicketID = (SELECT ticketID FROM DispatchRatedTickets WHERE ratingID = @thisRatingID)
SET @thisChargeID = (SELECT chargeID FROM DispatchRatedTickets WHERE ratingID = @thisRatingID)
SET @thisManifestID = (SELECT DISTINCT manifestID FROM DispatchManifestTickets WHERE ticketID = @thisTicketID)
SET @thisDriverID = (SELECT driverID FROM DispatchManifests WHERE manifestID = @thisManifestID)
SET @thisCompanyID = (SELECT companyID FROM DispatchDrivers WHERE driverID = @thisDriverID)
SET @newCost = (SELECT chargeCost FROM DispatchCosts WHERE companyID = @thisCompanyID AND chargeID = @thisChargeID)
SET @newAmount = (SELECT chargeAmount FROM DispatchCharges WHERE chargeID = @thisChargeID)

-- Set Ticket Destination PostalCode & Weight
Set @thisPostalCode = (SELECT postalCode FROM DispatchTickets WHERE ticketID = @thisTicketID)
Set @thisTotalWeight = (SELECT totalWeight FROM DispatchTickets WHERE ticketID = @thisTicketID)

-- Set the rate for the code based on the total weight of the shipment
Set @thisChargeRate = (SELECT chargeRate FROM DispatchRates WHERE postalCode = @thisPostalCode AND @thisTotalWeight >= chargeStart AND @thisTotalWeight <= chargeStop)
Set @thisChargeMin = (SELECT chargeMin FROM DispatchRates WHERE postalCode = @thisPostalCode AND @thisTotalWeight >= chargeStart AND @thisTotalWeight <= chargeStop)

-- Calculate freight charge
SET @thisFreightCharge = (@thisTotalWeight * @thisChargeRate / 100)

-- Compare to minimum charge & adjust if needed
IF (@thisFreightCharge < @thisChargeMin) BEGIN
SET @thisFreightCharge = @thisChargeMin
END

-- Calculate freight cost
IF (@thisCompanyID <> 1) BEGIN
IF (@thisTotalWeight < 954) BEGIN
SET @thisFreightCost = (@thisTotalWeight * 0.0516)
END
ELSE IF (@thisTotalWeight >= 954 AND @thisTotalWeight <= 1934) BEGIN
SET @thisFreightCost = (@thisTotalWeight * 0.0492)
END
ELSE IF (@thisTotalWeight >= 1935 AND @thisTotalWeight <= 3465) BEGIN
SET @thisFreightCost = (@thisTotalWeight * 0.0476)
END
ELSE IF (@thisTotalWeight >= 3466) BEGIN
SET @thisFreightCost = (@thisTotalWeight * 0.0330)
END
IF (@thisFreightCost > 9.69) BEGIN
SET @thisFreightCost = 9.69
END
END
ELSE BEGIN
SET @thisFreightCost = (@thisFreightCharge * 0.80)
END

IF (@thisChargeID = 0) BEGIN
Set @newChargeAmount = @thisFreightCharge
Set @newChargeCost = @thisFreightCost
END
ELSE IF (@thisChargeID = 15) BEGIN
Set @newChargeAmount = @newAmount * 0.05
Set @newChargeCost = 0
END
ELSE BEGIN
Set @newChargeAmount = @newAmount * @thisChargeQuantity
Set @newChargeCost = @newCost * @thisChargeQuantity
END

UPDATE DispatchRatedTickets
SET chargeAmount = @newChargeAmount
,chargeCost = @newChargeCost
WHERE ratingID = @thisRatingID

END
Currently, the code in red could return more than one result and would throw an error. Thanks for your help!!!

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-12-01 : 08:59:41
From BOL
quote:
IF (COLUMNS_UPDATED())

Tests to see, in an INSERT or UPDATE trigger only, whether the mentioned column or columns were inserted or updated. COLUMNS_UPDATED returns a varbinary bit pattern that indicates which columns of the table were inserted or updated.

COLUMNS_UPDATED can be used anywhere inside the body of the trigger.




*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
A candle loses nothing by lighting another candle
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-12-01 : 09:03:03
quote:
I realize the triggers fire in batches and fully understand the way that the virtual tables of INSERTED and DELETED are used. However, I need to find a value within the "current" row that is being updated/inserted
Ummmm, sorry, you missed the point. There is no "current" row of an UPDATE or INSERT operation. The entire set is done in one operation. You can work on the SET of rows affected. If you want to work on one row at a time (bad idea) you would have to use a cursor (very bad idea, and in a trigger, the WORST POSSIBLE idea) I know Oracle and perhaps other DBs have triggers that work one row at a time, but it's not an efficient process and it's a bad concept to try to use in SQL Server, or any database for that matter.

Unfortunately you've written your trigger to work one row at a time, and if you want it to work on multiple rows you'll need to change it significantly. This isn't a bad thing, since it will still work with single-row UPDATEs and INSERTs.
Go to Top of Page

FastCougar
Starting Member

3 Posts

Posted - 2004-12-01 : 09:04:30
Yes, I found that in BOL as well, but how would I use that to get the chargeID?
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-12-01 : 09:11:11
http://www.databasejournal.com/features/mssql/article.php/1479821

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
A candle loses nothing by lighting another candle
Go to Top of Page

FastCougar
Starting Member

3 Posts

Posted - 2004-12-01 : 09:36:14
quote:
Originally posted by tuenty

http://www.databasejournal.com/features/mssql/article.php/1479821

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
A candle loses nothing by lighting another candle


Unfortuneately, this will not tell me the value, only that the column was updated. How would I use this to find the value of the updated column?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-12-01 : 09:46:43
select COLUMN from INSERTED

- Jeff
Go to Top of Page
   

- Advertisement -