You are on page 1of 28

SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put_line('Welcome to PLSQL'); 3* end; SQL> / PL/SQL procedure successfully completed.

SQL> set serveroutput on SQL> / Welcome to PLSQL PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 v1 number; 3 v2 number := 10; 4 v3 varchar2(15); 5 v4 emp.job%type; 6 begin 7 select empno,ename,job,sal into v1,v3,v4,v2 from emp where empno=7839; 8 dbms_output.put_line(v1||' '||v3||' '||v4||' '||v2); 9 dbms_output.put_line(sql%rowcount); 10* end; SQL> / 7839 KING PRESIDENT 5000 1 PL/SQL procedure successfully completed.

To know the outcome of No.Of rows reflected: ROWCOUNT IS USED


SQL> ed Wrote file afiedt.buf 1 2 3 4 5 6 7 declare v1 number; v2 number := 10; v3 varchar2(15); v4 emp.job%type; begin select empno,ename,job,sal into v1,v3,v4,v2 from emp where empno=7839;

8 dbms_output.put_line(v1||' '||v3||' '||v4||' '||v2); 9 dbms_output.put_line(sql%rowcount); 10 delete emp where deptno=20; 11 dbms_output.put_line('Delete statement outcome is'||' ' ||sql%rowcount); 12* end; SQL> / 7839 KING PRESIDENT 5000 1 Delete statement outcome is 5 PL/SQL procedure successfully completed. SQL> delete emp where deptno=30; 6 rows deleted. SQL> begin 2 delete emp where deptno=20; 3 end; 4 / PL/SQL procedure successfully completed.

Example for displaying specific rows:


SQL> ed Wrote file afiedt.buf declare TYPE emp_record IS RECORD ( vempno number, vename emp.ename%type, vjob varchar2(15), vsal emp.sal%type, vhiredate emp.hiredate%type ); v1 emp_record; begin select empno,ename,job,sal,hiredate into v1 from emp where empno=7839; dbms_output.put_line(v1.vempno||' '||v1.vename||' '||v1.vjob||' '||v1.vjob||' '||v1.vsal||' '||v1.vhiredate); end; / 7839 KING PRESIDENT PRESIDENT 5000 17-NOV-81 PL/SQL procedure successfully completed.

Example for displaying multiple rows at a time:


SQL> ed Wrote file afiedt.buf 1 declare 2 v1 emp%rowtype; 3 begin 4 select * into v1 from emp where empno=7788; 5 dbms_output.put_line(v1.ename||' '||v1.job||' '||v1.sal); 6* end; SQL> / SCOTT ANALYST 3000 PL/SQL procedure successfully completed.

Use of IF statement:
SQL> ed Wrote file afiedt.buf 1 declare 2 v1 emp%rowtype; 3 begin 4 select * into v1 from emp where empno=7788; 5 if v1.sal>4000 6 then 7 dbms_output.put_line('Decent salary'); 8 elsif v1.sal>2000 and v1.sal<4000 9 then 10 dbms_output.put_line('Decent salary'); 11 else 12 dbms_output.put_line('Avg salary'); 13 END IF; 14* end; 15 / Decent salary PL/SQL procedure successfully completed.

Use of WHILE LOOP:


SQL> ed Wrote file afiedt.buf 1 declare 2 avgsal number; 3 begin 4 select avg(Sal) into avgsal from emp; 5 dbms_output.put_line('Average salary in the begining'||':'|| avgsal); 6 while avgsal<=7500 7 loop 8 update emp set sal=sal*1.1; 9 select avg(Sal) into avgsal from emp; 10 end loop; 11 dbms_output.put_line('Average salary at the end'||':'|| avgsal); 12* end; SQL> / Average salary in the begining:2073.214285714285714285714285714285714286 Average salary at the end:7873.046428571428571428571428571428571429 PL/SQL procedure successfully completed.

Use of EXIT WHEN LOOP:


SQL> ed Wrote file afiedt.buf 1 declare 2 avgsal number; 3 begin 4 select avg(Sal) into avgsal from emp; 5 dbms_output.put_line('Average salary in the begining'||':'' '|| avgsal); 6 loop 7 update emp set sal=sal*1.1; 8 select avg(Sal) into avgsal from emp; 9 exit when avgsal>=7500; 10 end loop; 11 dbms_output.put_line('Average salary at the end'||':'' '|| avgsal); 12* end; SQL> / Average salary in the begining:' 2280.535714285714285714285714285714285714 Average salary at the end:' 7873.046428571428571428571428571428571429 PL/SQL procedure successfully completed.

Use of FOR loop:


SQL> ed Wrote file afiedt.buf 1 begin 2 for var in (select * from emp) loop 3 dbms_output.put_line(var.ename||' '||var.deptno); 4 end loop; 5* end; SQL> / SMITH 20 ALLEN 30 WARD 30 JONES 20 MARTIN 30 BLAKE 30 CLARK 10 SCOTT 20 KING 10 TURNER 30 ADAMS 20 JAMES 30 FORD 20 MILLER 10 PL/SQL procedure successfully completed.

16/08/11: To fetch one row that is first:


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp; 3 v1 c1%rowtype; 4 begin 5 open c1; 6 fetch c1 into v1; 7 dbms_output.put_line(v1.empno||' '||v1.ename||' '||v1.deptno||' '||v1.job); 8* end; SQL> / 7369 SMITH 20 CLERK PL/SQL procedure successfully completed.

To display all rows using do while:


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp; 3 v1 c1%rowtype; 4 begin 5 open c1; 6 loop 7 fetch c1 into v1; 8 exit when c1%notfound; 9 dbms_output.put_line(v1.empno||' '||v1.ename||' '||v1.deptno||' '||v1.job); 10 end loop; 11* end; SQL> / 7369 SMITH 20 CLERK 7499 ALLEN 30 SALESMAN 7521 WARD 30 SALESMAN 7566 JONES 20 MANAGER 7654 MARTIN 30 SALESMAN 7698 BLAKE 30 MANAGER 7782 CLARK 10 MANAGER 7788 SCOTT 20 ANALYST 7839 KING 10 PRESIDENT 7844 TURNER 30 SALESMAN 7876 ADAMS 20 CLERK 7900 JAMES 30 CLERK 7902 FORD 20 ANALYST

7934 MILLER 10 CLERK PL/SQL procedure successfully completed.

To display first 3 rows using for loop:


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp; 3 v1 c1%rowtype; 4 begin 5 open c1; 6 for var in 1..3 loop 7 fetch c1 into v1; 8 dbms_output.put_line(v1.empno||' '||v1.ename||' '||v1.deptno||' '||v1.job); 9 end loop; 10* end; SQL> / 7369 SMITH 20 CLERK 7499 ALLEN 30 SALESMAN 7521 WARD 30 SALESMAN PL/SQL procedure successfully completed.

To display only the 3rd row using for loop:


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp; 3 v1 c1%rowtype; 4 begin 5 open c1; 6 for var in 1..3 loop 7 fetch c1 into v1; 8 end loop; 9 dbms_output.put_line(v1.empno||' '||v1.ename||' '||v1.deptno||' '||v1.job); 10* end; SQL> / 7521 WARD 30 SALESMAN PL/SQL procedure successfully completed.

FOR LOOP ,DONT NEED SQL> ed Wrote file afiedt.buf

OPEN, FETCH, CLOSE (ALL IMPLICIT)

1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp where sal>1200; 3 v1 c1%rowtype; 4 begin 5 for var in c1 loop 6 dbms_output.put_line(var.empno||' '||var.ename||' '||var.deptno||' '||var.job); 7 end loop; 8* end; SQL> / 7499 ALLEN 30 SALESMAN 7521 WARD 30 SALESMAN 7566 JONES 20 MANAGER 7654 MARTIN 30 SALESMAN 7698 BLAKE 30 MANAGER 7782 CLARK 10 MANAGER 7788 SCOTT 20 ANALYST 7839 KING 10 PRESIDENT 7844 TURNER 30 SALESMAN 7902 FORD 20 ANALYST 7934 MILLER 10 CLERK PL/SQL procedure successfully completed. FOR LOOP ,DONT NEED SQL> ed Wrote file afiedt.buf

DECLARE, OPEN, FETCH,

CLOSE (ALL IMPLICIT)

1 begin 2 for c1 in (select * from emp) loop 3 dbms_output.put_line(c1.empno||' '||c1.ename||' '||c1.deptno||' '||c1.job); 4 end loop; 5* end; SQL> / 7369 SMITH 20 CLERK 7499 ALLEN 30 SALESMAN 7521 WARD 30 SALESMAN 7566 JONES 20 MANAGER 7654 MARTIN 30 SALESMAN 7698 BLAKE 30 MANAGER 7782 CLARK 10 MANAGER 7788 SCOTT 20 ANALYST 7839 KING 10 PRESIDENT

7844 TURNER 30 SALESMAN 7876 ADAMS 20 CLERK 7900 JAMES 30 CLERK 7902 FORD 20 ANALYST 7934 MILLER 10 CLERK PL/SQL procedure successfully completed.

Select the particular row and update:


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp where sal>1200 for update; 3 var c1%rowtype; 4 begin 5 open c1; 6 for i in 1..5 loop 7 fetch c1 into var; 8 end loop; 9 update emp set sal=9999 where current of c1; 10 dbms_output.put_line(var.empno||' '||var.ename||' '||var.deptno||' '||var.job||' '||var.sal); 11* end; SQL> / 7698 BLAKE 30 MANAGER 2850 PL/SQL procedure successfully completed.

TO reflect the value and display it


SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1 is select empno,ename,deptno,job,sal from emp where sal>1200 for update; 3 var c1%rowtype; 4 begin 5 open c1; 6 for i in 1..5 loop 7 fetch c1 into var; 8 end loop; 9 update emp set sal=9999 where current of c1; 10 dbms_output.put_line('Old emp details: '||var.empno||' '||var.ename||' '||var.deptno||' '||var. 11 close c1; 12 open c1; 13 for i in 1..5 loop 14 fetch c1 into var;

15 end loop; 16 dbms_output.put_line('New emp details: '||var.empno||' '||var.ename||' '||var.deptno||' '||var. 17* end; SQL> / Old emp details: 7698 BLAKE 30 MANAGER 2850 New emp details: 7698 BLAKE 30 MANAGER 9999 PL/SQL procedure successfully completed.

PARAMETERISED CURSORS:
SQL> ed Wrote file afiedt.buf 1 declare 2 cursor c1(dcode dept.deptno%type) is select empno,ename,deptno,job,sal from emp 3 where sal>1200 and deptno=dcode; 4 var c1%rowtype; 5 begin 6 open c1(&dcode); 7 loop 8 fetch c1 into var; 9 exit when c1%notfound; 10 dbms_output.put_line('Old emp details: '||var.empno||' '||var.ename||' '||var.deptno||' '|| 11 var.job||''||var.sal); 12 end loop; 13* end; SQL> / Enter value for dcode: 10 old 6: open c1(&dcode); new 6: open c1(10); Old emp details: 7782 CLARK 10 MANAGER2450 Old emp details: 7839 KING 10 PRESIDENT5000 Old emp details: 7934 MILLER 10 CLERK1300 PL/SQL procedure successfully completed.

PROCEDURES:
SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1 2 is 3 begin 4 dbms_output.put_line('Welcome to stored procedure'); 5* end; SQL> / Procedure created. INTENTIONALly CREATED ERROR: SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1 2 is 3 begin 4 bms_output.put_line('Welcome to stored procedure'); 5* end; SQL> / Warning: Procedure created with compilation errors. SQL> show errors; Errors for PROCEDURE PROC1: LINE/COL ERROR -------- ----------------------------------------------------------------4/1 PL/SQL: Statement ignored 4/1 PLS-00201: identifier 'BMS_OUTPUT.PUT_LINE' must be declared SQL> show error procedure proc1; Errors for PROCEDURE PROC1: LINE/COL ERROR -------- ----------------------------------------------------------------4/1 PL/SQL: Statement ignored 4/1 PLS-00201: identifier 'BMS_OUTPUT.PUT_LINE' must be declared SQL> SQL> select text from user_source where name='PROC1'; TEXT ------------------------------------------------------create or replace procedure proc1

is begin bms_output.put_line('Welcome to stored procedure'); end;

Procedure with single arguement:


SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1(ecode number) 2 IS 3 v1 emp%rowtype; 4 begin 5 select * into v1 from emp where empno=ecode; 6 dbms_output.put_line(v1.ename||' '||v1.job); 7* end; SQL> / Procedure created. SQL> exec proc1(7788); SCOTT ANALYST PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1(ecode number default 7788) 2 IS 3 v1 emp%rowtype; 4 begin 5 select * into v1 from emp where empno=ecode; 6 dbms_output.put_line(v1.ename||' '||v1.job); 7* end; SQL> / Procedure created. SQL> exec proc1 SCOTT ANALYST PL/SQL procedure successfully completed.

Procedure with multiple arguement: and Bind variables and IN and OUT mode sepeartely

SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1( 2 ecode in number default 7788, 3 ename out varchar2, 4 deptno out number, 5 sal out number) 6 IS 7 v1 emp%rowtype; 8 begin 9 select * into v1 from emp where empno=ecode; 10 ename:=v1.ename; 11 deptno:=v1.deptno; 12 sal:=v1.sal; 13* end; SQL> / Procedure created.

To see output:
METHOD 1:
SQL> variable v1 varchar2(15); --Bind variables SQL> variable v2 number; SQL> variable v3 number; SQL> exec proc1(7839,:v1,:v2,:v3); KING PRESIDENT PL/SQL procedure successfully completed.

METHOD 2:
SQL> ed Wrote file afiedt.buf 1 declare 2 x varchar2(15); 3 y number; 4 z number; 5 begin 6 proc1(7839,x,y,z); 7 dbms_output.put_line(x||' '||y||' '||z); 8* end; SQL> / KING 10 5000 PL/SQL procedure successfully completed.

METHOD 3:
SQL> print V1 -------------------------------KING V2 ---------10 V3 ---------5000 SQL> print v1; V1 -------------------------------KING

TO DESCRIBE THE PROCEDURE:


SQL> desc proc1; PROCEDURE proc1 Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------ECODE NUMBER IN DEFAULT ENAME VARCHAR2 OUT DEPTNO NUMBER OUT SAL NUMBER OUT

PARTICULAR FORMAT DISPLAY using IN/OUT mode:


SQL> ed Wrote file afiedt.buf 1 create or replace procedure proc1(phno in out varchar2) 2 is 3 begin 4 phno := '('||substr(phno,1,3)||')-'||substr(phno,4,8); 5* end; SQL> / Procedure created. SQL> exec :v1:='044654220'; PL/SQL procedure successfully completed. SQL> print :v1; V1 -------------------------------044654220 SQL> exec proc1(:v1); PL/SQL procedure successfully completed. SQL> print :v1; V1 -------------------------------(044)-654220 SQL> ed Wrote file afiedt.buf 1 2 3 4 5 6 7 8 9 create or replace procedure proc1( i number, j number, l varchar2) is v1 emp%rowtype; begin select * into v1 from emp where empno=i and deptno=j and ename=l; dbms_output.put_line(v1.ename||' '||v1.deptno||' '||v1.sal);

10* end; SQL> / Procedure created. IF we want to get the input as declared: (Positional Method) SQL> exec proc1(7839,10,'KING'); KING 10 5000 PL/SQL procedure successfully completed. IF we want to get the input as we want: (Positional(1st arg) + Naming(other args) = COMBINATION SQL> exec proc1(7839,L=>'KING',J=>10); KING 10 5000 PL/SQL procedure successfully completed.

FUNCTIONS:
SQL> ed Wrote file afiedt.buf 1 create or replace function itax(salary number) 2 return number 3 is 4 begin 5 return salary*0.1; 6* end itax; SQL> / Function created. SQL> desc itax; FUNCTION itax RETURNS NUMBER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------SALARY NUMBER IN SQL> exec :v3 := itax(6800); PL/SQL procedure successfully completed. SQL> print :v3; V3 ---------680 SQL> select ename,sal,itax(sal) from emp; ENAME SAL ITAX(SAL) ---------- ---------- ---------SMITH 800 80 ALLEN 1600 160 WARD 1250 125 JONES 2975 297.5 MARTIN 1250 125 BLAKE 2850 285 CLARK 2450 245 SCOTT 3000 300 KING 5000 500 TURNER 1500 150 ADAMS 1100 110 ENAME SAL ITAX(SAL) ---------- ---------- ----------

JAMES FORD MILLER

950 3000 1300

95 300 130

14 rows selected.

Functions and procedures.... Call from procedures to functions. To check whether the sal is in the range
SQL> ed Wrote file afiedt.buf 1 create or replace function check_sal( 2 ecode emp.empno%type, 3 salary number 4 ) 5 return boolean 6 is 7 lsal number; 8 hsal number; 9 begin 10 select losal,hisal into lsal,hsal from salgrade,emp 11 where sal between losal and hisal and empno=ecode; 12 if salary between lsal and hsal 13 then 14 return true; 15 else 16 return false; 17 end if; 18* end check_sal; SQL> / Function created. SQL> ed Wrote file afiedt.buf 1 create or replace procedure upd_sal( 2 ecode emp.empno%type, 3 salary emp.sal%type 4 ) 5 is 6 begin 7 if check_sal(ecode,salary) 8 then 9 update emp set sal=salary where empno=ecode; 10 else

11 dbms_output.put_line('Salary not in the range'); 12 end if; 13* end upd_sal; SQL> / Procedure created. SQL> ed Wrote file afiedt.buf 1* update emp set sal=8000 where empno=7788 --It should not update as 8000 is not in the range SQL> / 1 row updated. SQL> roll; Rollback complete. SQL> exec upd_sal(7788,1000); Salary not in the range PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1* select empno,ename,grade,sal,losal,hisal from emp,salgrade where sal between losal and hisal SQL> / EMPNO ENAME GRADE SAL LOSAL ---------- ---------- ---------- ---------- ---------- ---------7788 SCOTT 4 2500 2001 3000 SQL> exec upd_sal(7788,2500); PL/SQL procedure successfully completed. HISAL

17/08/11
The OUT parameters are passed by reference. The IN parameters are passed by Value. IN is used when the environment need to send the values to the program. OUT is used to send the values to the calling environment.

Package and package body:


1) SQL> create or replace package p1 2 is 3 v1 number; 4 v2 number; 5 v3 varchar2(25); 6 v4 varchar2(20); 7 v5 date; 8 cursor c1(dno number) is select * from emp where deptno=dno; 9 emp_rec c1%rowtype; 10 procedure proc1(deptno number); 11 function disp_emp(ecode number) return number; 12 end; 13 / Package created. SQL> ed Wrote file afiedt.buf 1 create or replace package body p1 2 is 3 procedure proc1(deptno number) 4 is 5 begin 6 open c1(deptno); 7 loop 8 fetch c1 into emp_Rec; 9 exit when c1%notfound; 10 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 11 end loop; 12 end; 13 function disp_emp(ecode number) return number 14 is 15 begin 16 select * into emp_rec from emp where empno=ecode;

17 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 18 return 1; 19 end; 20* end p1; SQL> / Package body created. SQL> exec p1.proc1(10); CLARK 10 2450 KING 10 5000 MILLER 10 1300 PL/SQL procedure successfully completed. SQL> exec p1.v1 := p1.disp_emp(7788); SCOTT 20 3000 PL/SQL procedure successfully completed. SQL> exec dbms_output.put_line(p1.v1); 1 PL/SQL procedure successfully completed.

ONE TIME ONLY PROCEDURE = ANONYMOUS BLOCK INSIDE THE PACKAGE BODY WITHOUT END
SQL> ed Wrote file afiedt.buf 1 create or replace package body p1 2 is 3 procedure proc1(deptno number) 4 is 5 begin 6 open c1(deptno); 7 loop 8 fetch c1 into emp_Rec; 9 exit when c1%notfound; 10 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 11 end loop; 12 end; 13 function disp_emp(ecode number) return number 14 is 15 begin 16 select * into emp_rec from emp where empno=ecode; 17 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal);

18 return 1; 19 end; 20 begin 21 dbms_output.put_line('One time procedure executed'); 22 v1:=5000; 23 v4:=sysdate; 24* end p1; SQL> / Package body created. SQL> set serveroutput on; SQL> exec dbms_output.put_line(p1.v1); One time procedure executed 5000 PL/SQL procedure successfully completed. SQL> exec dbms_output.put_line(p1.v4); 17-AUG-11 PL/SQL procedure successfully completed. PRIVATE MEMBER SQL> ed Wrote file afiedt.buf 1 create or replace package body p1 2 is 3 procedure prvt 4 is 5 begin 6 dbms_output.put_line('Private member of a package'); 7 end; 8 procedure proc1(deptno number) 9 is 10 begin 11 prvt; --Invoking private member 12 open c1(deptno); 13 loop 14 fetch c1 into emp_Rec; 15 exit when c1%notfound; 16 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 17 end loop; 18 end; 19 function disp_emp(ecode number) return number 20 is 21 begin

22 select * into emp_rec from emp where empno=ecode; 23 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 24 return 1; 25 end; 26 begin 27 dbms_output.put_line('One time procedure executed'); 28 v1:=5000; 29 v4:=sysdate; 30* end p1; SQL> / Package body created. SQL> exec p1.proc1(10); One time procedure executed Private member of a package CLARK 10 2450 KING 10 5000 MILLER 10 1300 PL/SQL procedure successfully completed. FORWARD DECLARATION: SQL> ed Wrote file afiedt.buf 1 create or replace package body p1 2 is 3 procedure prvt; 4 procedure proc1(deptno number) 5 is 6 begin 7 prvt; --Invoking private member 8 open c1(deptno); 9 loop 10 fetch c1 into emp_Rec; 11 exit when c1%notfound; 12 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 13 end loop; 14 end; 15 function disp_emp(ecode number) return number 16 is 17 begin 18 select * into emp_rec from emp where empno=ecode; 19 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 20 return 1; 21 end; 22 procedure prvt

23 is 24 begin 25 dbms_output.put_line('Private member of a package'); 26 end; 27 begin 28 dbms_output.put_line('One time procedure executed'); 29 v1:=5000; 30 v4:=sysdate; 31* end p1; SQL> / Package body created. SQL> exec p1.proc1(10); One time procedure executed Private member of a package CLARK 10 2450 KING 10 5000 MILLER 10 1300 PL/SQL procedure successfully completed.

Function Overloading:
SQL> ed Wrote file afiedt.buf 1 create or replace package p1 2 is 3 v1 number; 4 v2 number; 5 v3 varchar2(25); 6 v4 varchar2(20); 7 v5 date; 8 cursor c1(dno number) is select * from emp where deptno=dno; 9 emp_rec c1%rowtype; 10 procedure proc1(deptno number); 11 function disp_emp(ecode number) return number; 12 function add(i number,j number) return number; 13 function add(i number,j number, k number) return number; 14 function add(i number,j number,k number, l number) return number; 15* end; SQL> / Package created. SQL> save p1 replace; Wrote file p1.sql SQL> create or replace package body p1

2 is 3 procedure proc1(deptno number) 4 is 5 begin 6 open c1(deptno); 7 loop 8 fetch c1 into emp_Rec; 9 exit when c1%notfound; 10 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 11 end loop; 12 end; 13 function disp_emp(ecode number) return number 14 is 15 begin 16 select * into emp_rec from emp where empno=ecode; 17 dbms_output.put_line(emp_rec.ename||' '||emp_rec.deptno||' '||emp_rec.sal); 18 return 1; 19 end; 20 function add(i number,j number) return number 21 is 22 begin 23 return i+j; 24 end; 25 function add(i number,j number,k number) return number 26 is 27 begin 28 return i+j+k; 29 end; 30 function add(i number,j number,k number,l number) return number 31 is 32 begin 33 return i+j+k+l; 34 end; 35 end p1; 36 / Package body created. SQL> exec p1.v1 := p1.add(10,20); PL/SQL procedure successfully completed. SQL> exec dbms_output.put_line(p1.v1); 30 PL/SQL procedure successfully completed. SQL> exec p1.v2 := p1.add(10,20,30);

PL/SQL procedure successfully completed. SQL> exec dbms_output.put_line(p1.v2); 60 PL/SQL procedure successfully completed. SQL> exec p1.v2 := p1.add(10,20,30,40); PL/SQL procedure successfully completed. SQL> exec dbms_output.put_line(p1.v2); 100 PL/SQL procedure successfully completed.

NEED FOR DYNAMIC SQL:


SQL> ed Wrote file afiedt.buf 1 begin 2 execute immediate 'create table emp_test as select * from emp'; 3 delete emp_test where deptno=10; 4 commit; 5* end; SQL> / delete emp_test where deptno=10; * ERROR at line 3: ORA-06550: line 3, column 10: PL/SQL: ORA-00942: table or view does not exist ORA-06550: line 3, column 3: PL/SQL: SQL Statement ignored (Since at compile time the ' execute immediate' syntax is checked, Its correct. Then, delete syntax is checked, it doesnot find emp_test, So we r gng to dynamic SQL, which skips the compile time errors and perform run time errors)

USE OF DYNAMIC SQL: (Another method) create or replace function delete_all(tablename varchar2) return number is csr_id integer; rows_del number; begin csr_id := dbms_sql.open_cursor; dbms_sql.parse(csr_id,'delete '||tablename,dbms_sql.native); rows_del := dbms_sql.execute(csr_id); dbms_sql.close_cursor(csr_id); return rows_del; end; EXCEPTION: TO execute the program by hiding the error: SQL> ed Wrote file afiedt.buf 1 declare 2 x number; 3 begin 4 select sal into x from emp; 5 exception 6 when others then null; 7* end; SQL> / PL/SQL procedure successfully completed. Actual Program error: SQL> ed Wrote file afiedt.buf 1 declare 2 x number; 3 begin 4 select sal into x from emp; 5 --exception 6 --when others then null; 7* end; SQL> / declare * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 4