Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming Using Oracle 11g

Similar presentations


Presentation on theme: "Database Programming Using Oracle 11g"— Presentation transcript:

1 Database Programming Using Oracle 11g
Collection

2 Collection Collection Similar to Arrays with some difference.
Collection can hold multiple data of similar datatype. Data is accessed through index. Size of collection is dynamic. Indexes are can be random

3 Collection PL/SQL Tables or Associated Array
Associate Array is a pair of key / value pair Key is string or integer index. Value is accessed through key Key is unique

4 Collection PL/SQL Tables or Associated Array
Index can be or can’t be sequential Size is dynamic or with very high limit Index can be sparse or dense Index can be number or string

5 Collection Syntax of PL/SQL Tables

6 Records Step-1: TYPE table_type_name IS TABLE OF datatype [NOT NULL] INDEX BY BINARY_INTEGER; Binary_integer: It store less than number Range: to Step-2: Variable of type Table_type_name

7 Collection Syntax of PL/SQL Tables Value of PL/SQL Table is accessed :
PL/SQL Table name (i) i:=Index.

8 Collection Implementing PL/SQL Tables -I

9 Records DECLARE TYPE JobTabTyp IS TABLE OF emp.ename%type
INDEX BY BINARY_INTEGER; job_tab JobTabTyp; -- declare local PL/SQL table job_title emp.job%TYPE; designation varchar2(16):='Prog'; counter number(10):=0; BEGIN

10 Records loop job_tab(counter) :=designation; dbms_output.put_line (job_tab(counter)); counter:=counter+1; exit when counter >100; end loop; END;

11 Collection PL/SQL Tables and Attributes
Attributes are available with PL/SQL tables which make them easy to use. There are 7 attributes

12 Collection PL/SQL Tables and Attributes
Attributes are available with PL/SQL tables which make them easy to use. There are 7 attributes Some need parameters Some act like procedure like delete

13 Collection Syntax of using PL/SQL Table with attributes

14 Records plsql_table_name{ . FIRST | . NEXT |
. DELETE[(index[, index])] EXISTS(index) | . COUNT | . NEXT(index) | . PRIOR(index)}

15 Collection PL/SQL Tables and Attributes
Any or no attribute can be used with PL/SQL tables

16 Collection PL/SQL Table and First and Next Attribute
First return first index number in the PL/SQL table Last attribute return index last number of the PL/SQL table. Parameter is required in next attribute

17 Collection PL/SQL Table and Count Attribute
Count is a numeric attribute which return total number of index created. Useful because index are not sequential by default.

18 Collection PL/SQL Table and Exist Attribute
Index of the PL/SQL table can be dense or sparse Exists return true if there is value on the particular index otherwise it will return false

19 Collection PL/SQL Table and Delete Attribute
It can delete particular entry from PL/SQL table Can be used with name of PL/SQL table

20 Collection Implementing PL/SQL Table -II

21 Collection DECLARE cursor c1 is select ename from emp where sal <3000; type c2 is table of emp.ename%type index by binary_integer; c3 c2; counter number(10):=0; begin

22 Collection for i in c1 loop counter :=counter+2; c3(counter):=i.ename;
dbms_output.put_line (c3(counter)); end loop; end;

23 Collection Implementing PL/SQL Table -III

24 Collection DECLARE cursor c1 is select ename from emp;
type c2 is table of emp.ename%type index by binary_integer; c3 c2; counter number(10):=0; b number(10):=0; begin for i in c1 loop counter :=counter+2;

25 Collection c3(counter):=i.ename; --dbms_output.put_line (c3(counter));
end loop; dbms_output.put_line ('Value of counter : ' || counter); dbms_output.put_line (c3.count()); while (b<counter) loop if c3.exists(b) then

26 Collection dbms_output.put_line ('Value exists at Index : '|| b);
end if ; b:=b+1; end loop; end;

27 Collection Implementing PL/SQL Table -IV

28 Collection Write a PL/SQL block to load all the Employee names into PL/SQL Table and copy the values in another PL/SQL Table before deleting those values from first PL/SQL Table

29 Collection DECLARE cursor c1 is select ename from emp;
type c2 is table of emp.ename%type index by binary_integer; c3 c2; c4 c2; counter number(10):=0; b number(10):=0; begin for i in c1 loop counter :=counter+2;

30 Collection c3(counter):=i.ename; --dbms_output.put_line (c3(counter));
end loop; dbms_output.put_line ('Value of counter : ' || counter); dbms_output.put_line (c3.count()); while (b<counter) loop if c3.exists(b) then dbms_output.put_line ('Value exists at Index : '|| b);

31 Collection c4(b):=c3(b); c3.delete(b);
dbms_output.put_line ('Value is deleted at Index '|| b); dbms_output.put_line ('Value copied in new PL/SQL Table '|| c4(b)); end if ; b:=b+1; end loop; end;

32 Collection Implementing PL/SQL Table and SQL-I

33 Collection Write a PL/SQL block to load all the Employee names into PL/SQL Table and insert the values into another table along with employee number and currentdate

34 Collection DECLARE cursor c1 is select ename from emp;
type c2 is table of emp.ename%type index by binary_integer; c3 c2; c4 c2; counter number(10):=0; b number(10):=0; begin for i in c1 loop

35 Collection counter :=counter+2; c3(counter):=i.ename;
--dbms_output.put_line (c3(counter)); end loop; dbms_output.put_line ('Value of counter : ' || counter); dbms_output.put_line (c3.count()); while (b<counter) loop if c3.exists (b) then

36 Collection insert into history values (b,c3(b),sysdate());
dbms_output.put_line ('Row is inserted using PL/SQL Table'); end if ; b:=b+1; end loop; end;


Download ppt "Database Programming Using Oracle 11g"

Similar presentations


Ads by Google