PL/SQL Record

download PL/SQL Record

of 6

Transcript of PL/SQL Record

  • 8/14/2019 PL/SQL Record

    1/6

    PL /SQL RECORD----------------------------Declare

    Type r1 is record(empno number(5),ename varchar2(10),hiredate date);

    r2 r1;Begin

    select empno,ename,hiredate into r2 from emp where empno=&Ask_Empno;dbms_output.put_line(r2.empno||' '||r2.ename||' '||r2.hiredate);

    Exceptionwhen no_data_found thenraise_application_error(-20023,'Invalid Employee Number');

    End;

    ------------------------------------------------------------------------------------------------------------------------------------

    With Procedure-----------------------

    set serverout on;create or replace procedure pr_plrecord (eno emp.empno%type) is

    Type r1 is record(empno number(5),ename varchar2(10),hiredate date);

    r2 r1;Begin

    Select empno,ename,hiredateinto r2from emp

    where empno=eno;dbms_output.put_line(r2.empno||' '||r2.ename||' '||r2.hiredate);

    Exceptionwhen no_data_found thenraise_application_error(-20023,'Invalid Employee Number');

    End;

    ------------------------------------------------------------------------------------------------------------------------------------

    With Cursor / Procedure-----------------------------------

    set serverout on;create or replace procedure pr1 (dno emp.deptno%type) is

    Type r1 is record(empno number(5),ename varchar2(10),

    hiredate date);r2 r1;

    cursor c1 isSelect empno,ename,hiredateFrom emp

    Where deptno=dno;Begin

    Open c1;Loop

  • 8/14/2019 PL/SQL Record

    2/6

    Exit when c1%notfound;Fetch c1 into r2;

    dbms_output.put_line(r2.empno||' '||r2.ename||' '||r2.hiredate);

    End Loop;Close c1;

    Exceptionwhen no_data_found thenraise_application_error(-20023,'Invalid Department Number');

    End;/show err

    ------------------------------------------------------------------------------------------------------------------------------------

    With Cursor / Procedure -- Joins----------------------------------------

    set serverout on;

    create or replace procedure pr2 (dno emp.deptno%type) isType r1 is record(empno number(5),

    ename varchar2(10),hiredate date,loc dept.loc%type);

    r2 r1;cursor c1 isSelect empno,ename,hiredate,locFrom emp,dept

    Where emp.deptno=dno and dept.deptno=emp.deptno;Begin

    Open c1;Loop

    Exit when c1%notfound;Fetch c1 into r2;

    dbms_output.put_line(r2.empno||' '||r2.ename||' '||r2.hiredate||' '||r2.loc);

    End Loop;Close c1;

    Exception

    when no_data_found thenraise_application_error(-20023,'Invalid Department Number');

    End;/show err

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    PL/SQL TABLE - 1-------------------------------

    set serverout on;Declare

  • 8/14/2019 PL/SQL Record

    3/6

    Type r1 is table of emp.ename%type index by binary_integer;r2 r1;i binary_integer:=0;

    BeginFor r3 in (Select ename from emp)

    Loopi:=i+1;

    r2(i):=r3.ename;dbms_output.put_line(r2(i));

    End Loop;End;/show err---------------------------------------------------------------------------------------------------------------------------------------------------------

    PL/SQL TABLE - 2-------------------------------

    DeclareType r1 is table of dept%rowtype index by Binary_Integer;r2 r1;

    Beginfor i in 1..10Loop

    select * into r2(i) from dept where deptno=10;dbms_output.put_line(r2(i).dname ||' '||r2(i).loc||'

    '||r2(i).deptno);End Loop;

    ExceptionWhen Too_many_rows Then

    Raise_Application_Error(-20023,'More than one rows');End;/

    =========================

    PL/SQL TABLE - 2A-------------------------------

    DeclareType r1 is table of dept%rowtype index by Binary_Integer;cursor c1 is select * from dept;r2 r1;

    i binary_integer:=0;dname varchar2(25);

    BeginFor a in c1Loop

    i:=i+1;dbms_output.put_line((i)||' '||a.deptno||' '||a.dname||'

    '||a.loc);dname:=a.dname;

  • 8/14/2019 PL/SQL Record

    4/6

    dbms_output.put_line(dname);End Loop;

    ExceptionWhen Too_many_rows Then

    Raise_Application_Error(-20023,'More than one rows');End;/

    ===================

    PL/SQL TABLE - 3-----------------------------Declare

    Type r1 is table of emp.ename%type index by Binary_Integer;r2 r1;cursor c1 is select ename from emp;i binary_integer:=0;

    BeginFor a in c1

    Loopi:=i+1;

    r2(i):=a.ename;dbms_output.put_line(r2(i));

    End Loop;End;/

    PL/SQL TABLE - 4-----------------------------set serverout on;create or replace procedure pr_pl1 (dno emp.deptno%type) is

    Type r1 is table of emp.ename%type index by binary_integer;r2 r1;i binary_integer:=0;

    BeginFor r3 in (Select ename from emp where deptno=dno)

    Loopi:=i+1;

    r2(i):=r3.ename;-- dbms_output.put_line(r2(i));

    dbms_output.put_line('Mr. '||r2(i));End Loop;

    Exceptionwhen no_data_found thenraise_application_error(-20023,'Invalid Department Number');

    End;/

    exec pr_pl1(20);exec pr_pl1(30);

    REF CURSOR - 1-----------------------------

  • 8/14/2019 PL/SQL Record

    5/6

    DeclareType re1 is Ref Cursor;a re1;b re1;a1 number;b1 varchar2(20);c1 varchar2(20);

    BeginOpen a for select empno,ename,job from emp;Loop

    fetch a into a1,b1,c1;exit when a%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);End Loop;Close a;

    End;/

    REF CURSOR - 2

    -----------------------------

    Create or Replace Procedure pref2 isType re1 is Ref Cursor;sql_stmt re1;a1 number;b1 varchar2(20);c1 varchar2(20);

    Begindbms_output.put_line('==========================');

    Open sql_stmt for select empno,ename,job from emp;Loop

    fetch sql_stmt into a1,b1,c1;exit when sql_stmt%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);End Loop;

    ----- Close sql_stmt;dbms_output.put_line('==========================');

    Open sql_stmt for select deptno,dname,loc from dept;Loop

    fetch sql_stmt into a1,b1,c1;exit when sql_stmt%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);End Loop;

    ------- Close sql_stmt;dbms_output.put_line('==========================');

    Open sql_stmt for select locid,loc_name,country from locmast;

    dbms_output.put_line('ID LocName Country');dbms_output.put_line('-----------------------------------------------

    -');Loop

    fetch sql_stmt into a1,b1,c1;exit when sql_stmt%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);

  • 8/14/2019 PL/SQL Record

    6/6

    End Loop;----- Close sql_stmt;End;/

    -----------------------------------------------------------------------------

    REF CURSOR - 3 [ With Procedure ]-----------------------------

    Create or Replace Procedure pref1(dno dept.deptno%type) isType re1 is Ref Cursor;sql_stmt re1;a1 number;b1 varchar2(20);c1 varchar2(20);

    Begindbms_output.put_line('==========================');

    Open sql_stmt for select deptno,dname,loc from dept where deptno=dno;

    Loopfetch sql_stmt into a1,b1,c1;exit when sql_stmt%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);End Loop;

    ------- Close sql_stmt;dbms_output.put_line('==========================');

    Open sql_stmt for select empno,ename,job from emp where deptno=dno;Loop

    fetch sql_stmt into a1,b1,c1;exit when sql_stmt%notfound;

    dbms_output.put_line(a1||' '||b1||' '||c1);End Loop;

    ----- Close sql_stmt;dbms_output.put_line('==========================');

    Exceptionwhen no_data_found thenraise_application_error(-20023,'Invalid Department Number');

    End;/

    Exec pref1(10);Exec pref1(20);