March 13, 2011 at 6:49 pm
HELLO!
I have two tables : product , tax
Product'fields: id amount Tax
1 150 ?
2 300 ?
TAX fields : Range_start ; Range_end ; TAX_Amount
Range_start Range_end Tax_Amount
100 200 10
201 300 25
301 400 30
For amount of 150 fees is 10
for amount of 201 fees is 25
help to write a trigger or stored procedure to select and insert tax_amount in product table when amount is inserted .
Thanks.
March 13, 2011 at 7:07 pm
THis is the proc:
IF OBJECT_ID('CalculateTax','P') IS NOT NULL
DROP PROCEDURE CalculateTax
GO
CREATE PROCEDURE CalculateTax
AS
BEGIN
UPDATE P
SET P.Tax = T.Tax_Amount
FROM Products P
JOIN Tax T
ON p.amount BETWEEN T.range_start AND T.range_end
END
damek_daddy, tel me honestly, isn't this part of your course homework?:w00t:
March 13, 2011 at 9:10 pm
Don't forget to update only where the Tax column is NULL so previous rows at possibly different tax rates aren't updated.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2011 at 12:23 am
ColdCoffee (3/13/2011)
THis is the proc:
UPDATE P
SET P.Tax = T.Tax_Amount
FROM Products P
JOIN Tax T
ON p.amount BETWEEN T.range_start AND T.range_end
END
Just a question on the bold part, I've actually never seen a join like this before using a between. Is this efficient to join like this, it just seems that it might be a bit slow for the join to use between. :unsure:
March 15, 2011 at 12:54 pm
Chris,
Using BETWEEN as a join condition is fairly common. It's really the same as using >= AND <= in either the join or WHERE clause. The efficiency depends on the same type of criteria as any other JOIN or WHERE clause - indexes and so forth.
Todd Fifield
March 15, 2011 at 11:06 pm
tfifield (3/15/2011)
Chris,Using BETWEEN as a join condition is fairly common. It's really the same as using >= AND <= in either the join or WHERE clause. The efficiency depends on the same type of criteria as any other JOIN or WHERE clause - indexes and so forth.
Todd Fifield
Never to old to learn!:-D
Been in It for 21 years, with about 10 years with SQL, can think that I've seen it, and have never used it.
Thanks:-)
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply