Create Function Sum2nos (A Number, B Number) Return Number Is S Number Begin S: A+b Return S End
Create Function Sum2nos (A Number, B Number) Return Number Is S Number Begin S: A+b Return S End
Procedure: It is nothing but a group of statements will be execute. It cannot return a value.
Function:
Create or Replace Function <function name>(<Arguments>) return <Datetype - Number / Varchar / Date> is
.........
Declaration Part; We can declare all the variable for this func.
..........
Begin
...........
executable part;
...........
return variable/value;
Exception
When <exception name> then
.... Error handling Statements ......
End;
Example:
.........
Example:
1. Create a plsql function to accept two numbers and return sum of those numbers.
Step 1: In Toad open New Sql window type the following code then select all the code
Step 2: Click on Execute as Script (or) Ctrl+<enter Key>
Step 3: Function should be created message without errors.
create function sum2nos(a number, b number)
return number is
s number;
begin
s:=a+b;
return s;
end;
Step 4: We can call this function any time or any number of times to passing two parameters as input it can return sum of
those numbers.
Step 5: Calling Above function in two methods.
a) By using sub program. Set Dbms_output on in Toad. Select the following code and run as script.
Declare
x number:=10;
y number:=75;
z number;
Begin
z:=sum2nos(x,y);
dbms_output.put_line('Sum is :'||z);
End;
b) By using the Select Statement.
Select sum2nos(400,200) from dual;
=> If you want to Open existing Function for Editing or Review the code.
1. Mouse right click on Connection (Apps)=> New => Schema Browser, Then select Functions tab.
2. Select your corresponding function name then code will be displayed in the right side editor it is in read only. If you
want to change anything in that code. Mouse Right click on the function name then select ' Load in the Editor'. then you
can edit the code and compile the code. It will be saved automatically in the database.
2. Write a function to accept 3 values as parameters and return sum.
3. Write a function to accept 2 values as parameters and return biggest value.
create function big2nos ( a number, b number) return number is
big number;
begin
if a>b then
big:=a;
else
big:=b;
end if;
return big;
end big2nos;
4. Write a function to accept deptno number and return total salaries in that deptno.
5. Write a function to accept 3 numbers and return big number.