34
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

PL SQL Practice

Embed Size (px)

Citation preview

Page 1: PL SQL Practice

SQL> edWrote file afiedt.buf

1 begin 2 dbms_output.put_line('Welcome to PLSQL'); 3* end;SQL> /

PL/SQL procedure successfully completed.

SQL> set serveroutput onSQL> /Welcome to PLSQL

PL/SQL procedure successfully completed.

SQL> edWrote 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 50001

PL/SQL procedure successfully completed.

To know the outcome of No.Of rows reflected: ROWCOUNT IS USEDSQL> edWrote 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;

Page 2: PL SQL Practice

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 50001Delete 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> edWrote file afiedt.bufdeclareTYPE emp_record IS RECORD(vempno number,vename emp.ename%type,vjob varchar2(15),vsal emp.sal%type,vhiredate emp.hiredate%type);v1 emp_record;beginselect 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.

Page 3: PL SQL Practice

Example for displaying multiple rows at a time:SQL> edWrote 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> edWrote 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.

Page 4: PL SQL Practice

Use of WHILE LOOP:SQL> edWrote 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.214285714285714285714285714285714286Average salary at the end:7873.046428571428571428571428571428571429

PL/SQL procedure successfully completed.

Use of EXIT WHEN LOOP:

SQL> edWrote 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.535714285714285714285714285714285714Average salary at the end:' 7873.046428571428571428571428571428571429

PL/SQL procedure successfully completed.

Page 5: PL SQL Practice

Use of FOR loop:SQL> edWrote 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 20ALLEN 30WARD 30JONES 20MARTIN 30BLAKE 30CLARK 10SCOTT 20KING 10TURNER 30ADAMS 20JAMES 30FORD 20MILLER 10

PL/SQL procedure successfully completed.

Page 6: PL SQL Practice

16/08/11:To fetch one row that is first:SQL> edWrote 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> edWrote 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 CLERK7499 ALLEN 30 SALESMAN7521 WARD 30 SALESMAN7566 JONES 20 MANAGER7654 MARTIN 30 SALESMAN7698 BLAKE 30 MANAGER7782 CLARK 10 MANAGER7788 SCOTT 20 ANALYST7839 KING 10 PRESIDENT7844 TURNER 30 SALESMAN7876 ADAMS 20 CLERK7900 JAMES 30 CLERK7902 FORD 20 ANALYST

Page 7: PL SQL Practice

7934 MILLER 10 CLERK

PL/SQL procedure successfully completed.

To display first 3 rows using for loop:SQL> edWrote 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 CLERK7499 ALLEN 30 SALESMAN7521 WARD 30 SALESMAN

PL/SQL procedure successfully completed.

To display only the 3rd row using for loop:SQL> edWrote 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.

Page 8: PL SQL Practice

FOR LOOP ,DONT NEED OPEN, FETCH, CLOSE (ALL IMPLICIT)SQL> edWrote file afiedt.buf

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 SALESMAN7521 WARD 30 SALESMAN7566 JONES 20 MANAGER7654 MARTIN 30 SALESMAN7698 BLAKE 30 MANAGER7782 CLARK 10 MANAGER7788 SCOTT 20 ANALYST7839 KING 10 PRESIDENT7844 TURNER 30 SALESMAN7902 FORD 20 ANALYST7934 MILLER 10 CLERK

PL/SQL procedure successfully completed.

FOR LOOP ,DONT NEED DECLARE, OPEN, FETCH, CLOSE (ALL IMPLICIT)SQL> edWrote file afiedt.buf

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 CLERK7499 ALLEN 30 SALESMAN7521 WARD 30 SALESMAN7566 JONES 20 MANAGER7654 MARTIN 30 SALESMAN7698 BLAKE 30 MANAGER7782 CLARK 10 MANAGER7788 SCOTT 20 ANALYST7839 KING 10 PRESIDENT

Page 9: PL SQL Practice

7844 TURNER 30 SALESMAN7876 ADAMS 20 CLERK7900 JAMES 30 CLERK7902 FORD 20 ANALYST7934 MILLER 10 CLERK

PL/SQL procedure successfully completed.

Select the particular row and update:SQL> edWrote 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> edWrote 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;

Page 10: PL SQL Practice

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 2850New emp details: 7698 BLAKE 30 MANAGER 9999

PL/SQL procedure successfully completed.

PARAMETERISED CURSORS:SQL> edWrote 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: 10old 6: open c1(&dcode);new 6: open c1(10);Old emp details: 7782 CLARK 10 MANAGER2450Old emp details: 7839 KING 10 PRESIDENT5000Old emp details: 7934 MILLER 10 CLERK1300

PL/SQL procedure successfully completed.

Page 11: PL SQL Practice

PROCEDURES:

SQL> edWrote 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> edWrote 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 ignored4/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 ignored4/1 PLS-00201: identifier 'BMS_OUTPUT.PUT_LINE' must be declaredSQL> SQL> select text from user_source where name='PROC1';

TEXT-------------------------------------------------------create or replace procedure proc1

Page 12: PL SQL Practice

isbeginbms_output.put_line('Welcome to stored procedure');end;

Procedure with single arguement:SQL> edWrote 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> edWrote 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 proc1SCOTT ANALYST

PL/SQL procedure successfully completed.

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

Page 13: PL SQL Practice

SQL> edWrote 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 variablesSQL> variable v2 number;SQL> variable v3 number;SQL> exec proc1(7839,:v1,:v2,:v3);KING PRESIDENT

PL/SQL procedure successfully completed.

METHOD 2:SQL> edWrote 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.

Page 14: PL SQL Practice

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

Page 15: PL SQL Practice

PARTICULAR FORMAT DISPLAY using IN/OUT mode:

SQL> edWrote 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> edWrote file afiedt.buf

1 create or replace procedure proc1( 2 i number, 3 j number, 4 l varchar2) 5 is 6 v1 emp%rowtype; 7 begin 8 select * into v1 from emp where empno=i and deptno=j and ename=l;

Page 16: PL SQL Practice

9 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.

Page 17: PL SQL Practice

FUNCTIONS:SQL> edWrote 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 80ALLEN 1600 160WARD 1250 125JONES 2975 297.5MARTIN 1250 125BLAKE 2850 285CLARK 2450 245SCOTT 3000 300KING 5000 500TURNER 1500 150ADAMS 1100 110

Page 18: PL SQL Practice

ENAME SAL ITAX(SAL)---------- ---------- ----------JAMES 950 95FORD 3000 300MILLER 1300 130

14 rows selected.

Functions and procedures.... Call from procedures to functions. To check whether the sal is in the range

SQL> edWrote 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> edWrote 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

Page 19: PL SQL Practice

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> edWrote file afiedt.buf

1* update emp set sal=8000 where empno=7788 --It should not update as 8000 is not in the rangeSQL> /

1 row updated.

SQL> roll;Rollback complete.

SQL> exec upd_sal(7788,1000);Salary not in the range

PL/SQL procedure successfully completed.

SQL> edWrote file afiedt.buf

1* select empno,ename,grade,sal,losal,hisal from emp,salgrade where sal between losal and hisalSQL> /

EMPNO ENAME GRADE SAL LOSAL HISAL---------- ---------- ---------- ---------- ---------- ---------- 7788 SCOTT 4 2500 2001 3000

SQL> exec upd_sal(7788,2500);

PL/SQL procedure successfully completed.

Page 20: PL SQL Practice

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> edWrote 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

Page 21: PL SQL Practice

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 2450KING 10 5000MILLER 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> edWrote 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

Page 22: PL SQL Practice

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 executed5000

PL/SQL procedure successfully completed.

SQL> exec dbms_output.put_line(p1.v4);17-AUG-11

PL/SQL procedure successfully completed.

PRIVATE MEMBER

SQL> edWrote 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

Page 23: PL SQL Practice

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 executedPrivate member of a packageCLARK 10 2450KING 10 5000MILLER 10 1300

PL/SQL procedure successfully completed.

FORWARD DECLARATION:

SQL> edWrote 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;

Page 24: PL SQL Practice

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 executedPrivate member of a packageCLARK 10 2450KING 10 5000MILLER 10 1300

PL/SQL procedure successfully completed.

Function Overloading:SQL> edWrote 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;

Page 25: PL SQL Practice

Wrote file p1.sqlSQL> 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.

Page 26: PL SQL Practice

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> edWrote 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 existORA-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)

Page 27: PL SQL Practice

USE OF DYNAMIC SQL: (Another method)

create or replace function delete_all(tablename varchar2) return numberiscsr_id integer;rows_del number;begincsr_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> edWrote 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> edWrote 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:

Page 28: PL SQL Practice

ORA-01422: exact fetch returns more than requested number of rowsORA-06512: at line 4