create a function that calculate tax 7% of salary & return it in main pl/sql code subtract that tax from salary.

by sikha tiwari
(India)

I cant understood this question.Exactly what to do here please give answer of this question.

Comments for create a function that calculate tax 7% of salary & return it in main pl/sql code subtract that tax from salary.

Average Rating starstarstarstarstar

Click here to add your own comments

Aug 26, 2014
Rating
starstarstarstarstar
how to vreate a pl/sql function
by: John

You don't really need a function for this as you can just multiply the salary by 0.07 (7 divided by 100) to determine the amount of tax that has to be subtracted from the salary.

However let's assume that using a function is a good idea and let's call our function tax. Now we need to write the function and let's also assume that it is going to be in the same module as the calling PL/SQL block so that we don't need to worry about private or public functions. Yet another assumption we will make is that in future the tax rate will change and that we have already retrieved the salary data from the database which means that we can pass it as a parameter to the function

With all those assumptions in place, our PL/SQL function would look something like this:


FUNCTION tax(salary NUMBER) RETURN NUMBER IS
tax_rate CONSTANT NUMBER := 0.07; /*7%^/
BEGIN
RETURN salary*tax_rate;
END;


And that is our function.

We can call it by using it as part of an expression in the main PL/SQL block like so:


tax_due := tax(salary);


That is a very simple example of a pl/sql function and in this case it's not even necessary to use a function. However it does demonstrate that we can declare local constants in functions and it is good practice to declare "magic£ numbers as constants so that they can be found changed very easily when the need arises.

For more detail on Oracle PL/SQL functions see our tutorial

Click here to add your own comments

Join in and write your own page! It's easy to do. How? Simply click here to return to Oracle Questions.