42 Replies - 1302 Views - Last Post: 10 September 2009 - 09:08 PM
#1
Help With Creating a Trigger
Posted 05 September 2009 - 02:24 PM
TaxReturns:
Name Null? Type
----------------------------------------- -------- ----------------------------
SSN NOT NULL CHAR(11)
ADJGROSSINCOME NUMBER(9)
INCOMETAXES NUMBER(9)
and
TaxRates:
Name Null? Type
----------------------------------------- -------- ----------------------------
MININCOME NOT NULL NUMBER(9)
MAXINCOME NUMBER(9)
TAXRATE NUMBER(3,2)
TaxReturns is empty.
TaxRates has the following rows:
MININCOME MAXINCOME TAXRATE
---------- ---------- ----------
0 29999 0
30000 99999 .2
100000 249999 .3
250000 499999 .4
500000 999999999 .5
I need to create a Trigger that runs whenever a new tax return is added or the AdjGrossIncome field of a tax return is updated. The trigger should compute the new tax amount based on the appropriate tax rate and place that amount in the IncomeTaxes field.
I've never made a Trigger before and haven't been able to purchase my databaseII book yet. I could use any help that anyone was willing to give.
Thanks so much!
Replies To: Help With Creating a Trigger
#2
Re: Help With Creating a Trigger
Posted 06 September 2009 - 08:16 AM
Anything? Anyone? Yooo hoooo!
Please help!
#3
Re: Help With Creating a Trigger
Posted 06 September 2009 - 09:04 AM
CREATE OR REPLACE TRIGGER Tax_Return_Trigger
AFTER INSERT OF TaxReturns_SSN_PK OR UPDATE OF AdjGrossIncome
ON TaxReturns
FOR EACH ROW
DECLARE
v_TaxRate1 := 0.00;
v_TaxRate2 := 0.20;
v_TaxRate3 := 0.30;
v_TaxRate4 := 0.40;
v_TaxRate5 := 0.50;
*edit* I think this is right?
This post has been edited by absynthe: 06 September 2009 - 10:00 PM
#4
Re: Help With Creating a Trigger
Posted 06 September 2009 - 09:44 PM
Now I just need the If Then for the different salaries etc I think
This post has been edited by absynthe: 06 September 2009 - 09:45 PM
#5
Re: Help With Creating a Trigger
Posted 06 September 2009 - 10:38 PM
ANY IDEAS? ANYONE? IS IT WRONG?
CREATE OR REPLACE TRIGGER Tax_Return_Trigger AFTER INSERT OF TaxReturns_SSN_PK OR UPDATE OF AdjGrossIncome ON TaxReturns FOR EACH ROW DECLARE v_TaxRate1 := 0.00; v_TaxRate2 := 0.20; v_TaxRate3 := 0.30; v_TaxRate4 := 0.40; v_TaxRate5 := 0.50; v_NewRate NUMBER; BEGIN IF AdjGrossIncome >= 0 AND AdjGrossIncome <= 29999 THEN v_NewRate := AdjGrossIncome * v_TaxRate1; ELSEIF AdjGrossIncome >= 30000 AND AdjGrossIncome <= 99999 THEN v_NewRate := AdjGrossIncome * v_TaxRate2; ELSEIF AdjGrossIncome >= 100000 AND AdjGrossIncome <= 249999 THEN v_NewRate := AdjGrossIncome * v_TaxRate3; ELSEIF AdjGrossIncome >= 250000 AND AdjGrossIncome <= 499999 THEN v_NewRate := AdjGrossIncome * v_TaxRate4; ELSEIF AdjGrossIncome >= 500000 AND AdjGrossIncome <= 999999999 THEN v_NewRate := AdjGrossIncome * v_TaxRate1; END IF; INSERT INTO TaxReturns (IncomeTaxes) VALUES (v_NewRate); END; /
This post has been edited by absynthe: 06 September 2009 - 10:56 PM
#6
Re: Help With Creating a Trigger
Posted 10 September 2009 - 12:23 PM
#7
Re: Help With Creating a Trigger
Posted 10 September 2009 - 05:28 PM
CREATE OR REPLACE TRIGGER Tax_Return_Trigger AFTER UPDATE OF AdjGrossIncome ON TaxReturns FOR EACH ROW DECLARE v_TaxRate1 := 0.00; v_TaxRate2 := 0.20; v_TaxRate3 := 0.30; v_TaxRate4 := 0.40; v_TaxRate5 := 0.50; v_AdjGrossIncome NUMBER; v_NewRate NUMBER; BEGIN SELECT AdjGrossIncome INTO v_AdjGrossIncome FROM TaxReturns; IF :NEW.AdjGrossIncome >= 0 AND :NEW.AdjGrossIncome <= 29999 THEN v_NewRate := :NEW.AdjGrossIncome * v_TaxRate1; ELSEIF :NEW.AdjGrossIncome >= 30000 AND :NEW.AdjGrossIncome <= 99999 THEN v_NewRate := :NEW.AdjGrossIncome * v_TaxRate2; ELSEIF :NEW.AdjGrossIncome >= 100000 AND :NEW.AdjGrossIncome <= 249999 THEN v_NewRate := :NEW.AdjGrossIncome * v_TaxRate3; ELSEIF :NEW.AdjGrossIncome >= 250000 AND :NEW.AdjGrossIncome <= 499999 THEN v_NewRate := :NEW.AdjGrossIncome * v_TaxRate4; ELSEIF :NEW.AdjGrossIncome >= 500000 AND :NEW.AdjGrossIncome <= 999999999 THEN v_NewRate := :NEW.AdjGrossIncome * v_TaxRate5; END IF; INSERT INTO TaxReturns (IncomeTaxes) VALUES (v_NewRate); END; SHOW ERRORS /
What are the compilation errors?
#8
Re: Help With Creating a Trigger
Posted 10 September 2009 - 05:32 PM
#9
Re: Help With Creating a Trigger
Posted 10 September 2009 - 05:58 PM
Any ideas? Anyone?
This post has been edited by absynthe: 10 September 2009 - 05:58 PM
#10
Re: Help With Creating a Trigger
Posted 10 September 2009 - 05:59 PM
#11
Re: Help With Creating a Trigger
Posted 10 September 2009 - 06:03 PM
Thats all it says unfortunately. When I update the table it doesnt calculate the value so the trigger is not working at all
#12
Re: Help With Creating a Trigger
Posted 10 September 2009 - 06:17 PM
#13
Re: Help With Creating a Trigger
Posted 10 September 2009 - 06:21 PM
v_TaxRate1 FLOAT := 0.00;
#14
Re: Help With Creating a Trigger
Posted 10 September 2009 - 06:28 PM
Thinking about chucking this computer out the window
#15
Re: Help With Creating a Trigger
Posted 10 September 2009 - 07:00 PM
|
|

New Topic/Question
Reply




MultiQuote





|