0% found this document useful (0 votes)
3 views27 pages

Records

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Records

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Database Programming Using Oracle

11g

Records
Records
Record
• Composite
Datatype

• Can hold more


than one piece
of information
as compare to
Scalar
Datatype like
number ,
varchars
• Single variable
Records

Type of Record

• Table Base

• Cursor Base

• User Defined
Records
Type of Record

• Records can
refer to all
columns of the
database
table.

• RowType in
used in Oracle
to create
table-based
records
Records

Implementing
Table-based
Records - I
Records
declare
emp_rec emp%rowtype;
begin
select * into emp_rec from emp where
empno=7369;
dbms_output.put_line (emp_rec.ename || '
' || emp_rec.job);
exception
when no_data_found then
dbms_output.put_line('No matching
record found');
end;
Records

Implementing
Table-based
Records - II
Records
DECLARE
emp_rec emp%ROWTYPE;
BEGIN
emp_rec.empno := 500;
emp_rec.ename := 'Special';
emp_rec.ename := 'Consultant';
emp_rec.mgr := 7369;
emp_rec.hiredate := sysdate;
emp_rec.sal := 2000;
emp_rec.comm := NULL;
emp_rec.deptno := 10;
Records

INSERT
INTO emp
VALUES emp_rec;
dbms_output.put_line('Record base
insertion is done');
END;
Records
Cursor-based
Records

• It can refer to
all columns of
the cursor
defined .

• RowType in
used in Oracle
to create
Cursor based
records
Records

Implementing
Cursor -based
Records - I
Records

Write a PL/SQL block which should


look for first occurrence of
employee either with designation
of CLERK or salary greater than
3000. Program should exit after
this displaying success message
Records
declare
cursor c1 is select * from emp;
c2 c1%rowtype;
begin
open c1;
loop
fetch c1 into c2;
if c2.sal > 2000 or c2.job='CLERK' then
dbms_output.put_line ('Value is matched');
exit
end if;
exit when c1%notfound;
end loop; end;
Records

Implementing
Cursor -based
Records - II
Records
declare
cursor c1 is select empno,ename,dname
from emp e, dept d where
e.deptno=d.deptno;
c2 c1%rowtype;
begin
open c1;
loop
fetch c1 into c2;
if c2.dname='SALES' then
update emp set sal = sal*0.20 + sal
where empno=c2.empno;
Records

dbms_output.put_line ('Salary is updated


by 20% of Employee no: ' || c2.empno);
end if;
exit when c1%notfound;
end loop;
end;
Records
User Defined
Record (UDR)
• UDR is
composite
datatype
• With UDR
programmer
have control
over defination
• Define
structure of
UDR
• Define variable
Records

Syntax of User
Defined Record
Records

Step# 1:

TYPE <type_name> IS RECORD


(<field_name1> <datatype1>,
<field_name2> <datatype2>,
... <field_nameN> <datatypeN> );

Step # 2:

Declare variable of Type_name;


Records
Syntax of User
Defined Record
(UDR)

• With UDR
programmer is
able to define
its own
structure

• Multiple
variables of
same UDR can
Records

Implementing
User Defined
Record - I
Records

Write a PL/SQL block to display the


information of first 5 employees
who are working as MAN in their
ename
Records
declare
type emp_rec is RECORD (empno
emp.empno%type, ename
emp.ename%type, sal emp.ename
%type);
emp_rec_val emp_rec;
cursor c1 is select empno, ename,
sal from emp where job like
('%MAN%');
begin
open c1;
Records

loop
fetch c1 into emp_rec_val;
dbms_output.put_line
(emp_rec_val.ename ||
emp_rec_val.sal );
exit when c1%rowcount > 5;
end loop;
end;
Records

Implementing
User Defined
Record - II
Records
declare
type emp_rec is RECORD (empno
emp.empno%type, ename
emp.ename%type, sal emp.ename
%type);
emp_rec_val emp_rec;
cursor c1 is select empno, ename,
sal from emp where job like
('%MAN%');
begin
open c1;
Records
loop
fetch c1 into emp_rec_val.empno,
emp_rec_val.ename,emp_rec_val.sal;
if emp_rec_val.ename ='BLAKE' then
insert into history values
(emp_rec_val.empno,
emp_rec_val.ename,sysdate());
dbms_output.put_line ('Data Added in
history table' );
end if ;
exit when c1%rowcount > 5;
end loop;
end;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy