Procedures and Functions - PDF
Procedures and Functions - PDF
Procedure created.
Showing errors
Warning: Procedure created with compilation errors.
LINE/COL ERROR
-------- -----------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol ";"
To execute Procedure
• Call procedure in a PL/SQL Program
DECLARE
sq number:=0;
x number:=&x;
BEGIN
squareNum(x,sq);
dbms_output.put_line(' Square is:'||sq);
END;
/ SQL> start F:\advdbms\2020\lab\program\procedure\call_sqr.sql;
Enter value for x: 4
old 3: x number:=&x;
new 3: x number:=4;
Square is:16
SQR
----------
64
Procedure with No Parameters
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE DisplaySalary IS
temp_Salary NUMBER(10,2);
temp_name emp.ename%type;
BEGIN
SELECT Sal,ename INTO temp_Salary,temp_name FROM emp WHERE
empno=102;
DBMS_OUTPUT.PUT_LINE ('Salary of '||temp_name||' is
'||temp_salary);
END;
/
Executing DisplaySalary Procedure
Procedure created.
Procedure created
Calling DisplaySalary1
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
displaysalary1(v_eno,v_sal);
dbms_output.put_line('Actual salary of employee '||v_eno||' is
'||v_sal);
END;
/
Executing
SQL> start F:\advdbms\2020\lab\program\procedure\call_dissal.sql;
Enter value for v_eno: 101
old 3: v_eno emp.empno%type:=&v_eno;
new 3: v_eno emp.empno%type:=101;
Actual salary of employee 101 is 30000
V_SAL
----------
55000
Procedure created.
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
v_sal:=display_salary(v_eno);
dbms_output.put_line('Salary of '||v_eno||' is '||v_sal);
END;
/
Executing Functions
PL/SQL Block
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
v_sal:=display_salary(v_eno);
dbms_output.put_line('Salary of '||v_eno||' is '||v_sal);
END;
/
SQL> start F:\advdbms\2020\lab\program\procedure\call_func.sql;
Enter value for v_eno: 103
old 3: v_eno emp.empno%type:=&v_eno;
new 3: v_eno emp.empno%type:=103;
Salary of 103 is 55000
Executing Functions
SELECT
DISPLAY_SALARY(103)
-------------------
55000
Executing Functions
EXECUTE/EXEC
Function created.
6 rows selected.
SQL> select display_ename('D2') from dual;
DISPLAY_ENAME('D2')
--------------------------------------------------------------------------------
Ravi
DISPLAY_ENAME('D1')
--------------------------------------------------------------------------------
Tina
Function Vs Procedure
Stored Procedure Function
May or may not returns a value to the calling Returns a value to the calling part of the
part of program. program.
Uses IN, OUT, IN OUT parameter. Uses only IN parameter.
Returns a value using “ OUT” parameter. Returns a value using “RETURN”.
Does not specify the datatype of the value if Necessarily specifies the datatype of the
it is going to return after a calling made to it. value which it is going to return after a
calling made to it.
Cannot be called from the function block of Can be called from the procedure block of
code. code.