I have a situation where I need to find a valid price for an object.
I have three tables:
Price with columns
PriceID int
ObjectID int
AccountID int
PriceDetail with columns
PriceDetailID int
PriceID int
ChargeRate float
Object with columns
ObjectID int
ParentObjectID int
The rules are:
Look up the ChargeRate for a given ObjectID and AccountID.
If the ChargeRate is NULL (i.e. there is no price for this Object and Account) then repeat the lookup using the ParentObjectID.
If the ParentObjectID IS NULL then it's the BaseObject and the lookup of the ChargeRate uses 0 as the AccountID.
Here's a scalar function that does the job:
[tt]
CREATE FUNCTION sufApplicablePrice
(
-- Add the parameters for the function here
@O INT,
@A INT
)
RETURNS float
AS
BEGIN
-- Declare the return variable here
DECLARE @Price float
-- Add the T-SQL statements to compute the return value here
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = @A AND P.ObjectID = @O
WHILE @Price IS NULL AND @O IS NOT NULL
BEGIN
SELECT @O = O.ParentObjectID FROM OBJECT O WHERE O.ObjectID = @O
IF @O IS NOT NULL
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = @A AND P.ObjectID = @O
ELSE
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = 0 AND P.ObjectID = @O
END
IF @Price IS NULL SET @Price = 0
-- Return the result of the function
RETURN @Price
END
[/tt]
I would like to know if there's any way I can avoid having to use a function. Recursive Common Table Expressions look promising but none of the examples seem to cover this type of case.
Bob Boffin
I have three tables:
Price with columns
PriceID int
ObjectID int
AccountID int
PriceDetail with columns
PriceDetailID int
PriceID int
ChargeRate float
Object with columns
ObjectID int
ParentObjectID int
The rules are:
Look up the ChargeRate for a given ObjectID and AccountID.
If the ChargeRate is NULL (i.e. there is no price for this Object and Account) then repeat the lookup using the ParentObjectID.
If the ParentObjectID IS NULL then it's the BaseObject and the lookup of the ChargeRate uses 0 as the AccountID.
Here's a scalar function that does the job:
[tt]
CREATE FUNCTION sufApplicablePrice
(
-- Add the parameters for the function here
@O INT,
@A INT
)
RETURNS float
AS
BEGIN
-- Declare the return variable here
DECLARE @Price float
-- Add the T-SQL statements to compute the return value here
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = @A AND P.ObjectID = @O
WHILE @Price IS NULL AND @O IS NOT NULL
BEGIN
SELECT @O = O.ParentObjectID FROM OBJECT O WHERE O.ObjectID = @O
IF @O IS NOT NULL
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = @A AND P.ObjectID = @O
ELSE
SELECT @Price = PD.ChargeRate FROM PRICE P LEFT OUTER JOIN PRICEDETAIL PD ON P.PriceID = PD.PriceID
WHERE P.AccountID = 0 AND P.ObjectID = @O
END
IF @Price IS NULL SET @Price = 0
-- Return the result of the function
RETURN @Price
END
[/tt]
I would like to know if there's any way I can avoid having to use a function. Recursive Common Table Expressions look promising but none of the examples seem to cover this type of case.
Bob Boffin