CREATE [OR REPLACE] FUNCTION function_name [parameters]
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
CREATE OR REPLACE FUNCTION adder(n1 IN number, n2 IN number)
RETURN number
IS
n3 number(8);
BEGIN
n3 :=n1+n2;
RETURN n3;
END;
-- To call the function --
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
DROP FUNCTION function_name
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS [declaration_section]
BEGIN
executable_section
EXCEPTION
exception_section
END procedure_name;
-- Table Creation --
CREATE TABLE user(id number(10) primary key,name varchar2(100));
-- Creating a Procedure --
CREATE OR REPLACE PROCEDURE "INSERTUSER"
(id IN NUMBER, name IN VARCHAR2)
IS
BEGIN
INSERT INTO user VALUES(id,name);
END;
-- Program to Call Procedure --
BEGIN
insertuser(101,'xyz');
dbms_output.put_line('record inserted successfully');
END;
DROP PROCEDURE name;
Procedure | Function |
---|---|
Used mainly to execute certain business logic with DML and DRL statements | Used mainly to perform some computational process and returning the result of that process. |
Procedure can return zero or more values as output. | Function can return only single value as output. |
Procedure cannot call with select statement, but can call from a block or from a procedure. | Function can call with select statement, if function does not contain any DML statements and DDL statements. function with DML and DDL statements can call with select statement with some special cases (using Pragma autonomous transaction) |
OUT keyword is used to return a value from procedure | RETURN keyword is used to return a value from a function |
It is not mandatory to return the value. | It is mandatory to return the value |
RETURN will simply exit the control from subprogram. | RETURN will exit the control from subprogram and also returns the value. |
Return datatype will not be specified at the time of creation. | Return datatype is mandatory at the time of creation |
Made By SOU Student for SOU Students