Vous êtes sur la page 1sur 4

set serveroutput on

declare
TYPE population IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
city_population population;
i varchar2(64);
begin
city_population('new york'):=10000;
city_population('las angels'):=20000;
city_population('chicago'):=30000;
city_population('las angels'):=50000;
i:=city_population.FIRST;
while i is not null loop
dbms_output.put_line('name of the city is '||i||' '||'and its population is '||
to_Char(city_population(i)));
i:=city_population.NEXT(i);
end loop;
end;
--------------------------------------------------------------------------------
---------------------
DECLARE
-- In the varray, put an upper limit on the number of elements
TYPE dnames_var IS VARRAY(10) OF VARCHAR2(30);
dept_names dnames_var;
BEGIN
-- Because dnames is declared as VARRAY(20),
-- you can put up to 10 elements in the constructor
dept_names := dnames_var('Shipping','Sales','Finance','Payroll','Payroll1','P
ayroll2','Payroll3','Payroll4','Payroll5');
for i in dept_names.first..dept_names.last
loop
dbms_output.put_line(dept_names(i));
end loop;
END;
------------------------------------------------------------
Example 5-12 Collection Constructor Including Null Elements
DECLARE
TYPE dnames_tab IS TABLE OF VARCHAR2(30);
dept_names dnames_tab;
TYPE dnamesNoNulls_type IS TABLE OF VARCHAR2(30) NOT NULL;
BEGIN
dept_names := dnames_tab('Shipping', NULL,'Finance', NULL);
-- If dept_names were of type dnamesNoNulls_type,
-- you could not include null values in the constructor
END;
----------------------------------------------------------------
If you call a constructor without arguments, you get an empty but non-null colle
ction as shown in Example 5-14.
DECLARE
TYPE dnames_var IS VARRAY(20) OF VARCHAR2(30);
dept_names dnames_var;
BEGIN
IF dept_names IS NULL THEN
DBMS_OUTPUT.PUT_LINE
('Before initialization, the varray is null.');
-- While the varray is null, you cannot check its COUNT attribute.
-- DBMS_OUTPUT.PUT_LINE
-- ('It has ' || dept_names.COUNT || ' elements.');
ELSE
DBMS_OUTPUT.PUT_LINE
('Before initialization, the varray is not null.');
END IF;
dept_names := dnames_var(); -- initialize empty varray
IF dept_names IS NULL THEN
DBMS_OUTPUT.PUT_LINE
('After initialization, the varray is null.');
ELSE
DBMS_OUTPUT.PUT_LINE
('After initialization, the varray is not null.');
DBMS_OUTPUT.PUT_LINE
('It has ' || dept_names.COUNT || ' elements.');
END IF;
END;
---------------------------------------------------------
declare
Type roster IS TABLE OF VARCHAR2(20);
NAMES roster:=roster('Johnson','peeter','rooter','water');
procedure verify_name(name1 VARCHAR2) IS
begin
dbms_output.put_line(name1);
end;
begin
for i in names.first..names.last
loop
if names(i)='Johnson' then
dbms_output.put_line('insid lf condition '||names(i));
end if;
end loop;
verify_name(names(3));
end;
----------------------------------------------------
declare
Type rec_type IS RECORD(R_NAME EMP.ENAME%TYPE,R_JOB EMP.JOB%TYPE);
Type arr IS varray(10) of rec_type;
salesperson arr;
cursor c1 IS select ename,job from emp where deptno=10;
Type nest IS Table of c1%ROWTYPE;
n nest;
encounter number:=5;
begin
salesperson:=arr();
select ename,job bulk collect into n FROM EMP;
if n.count > 0 then
if n.last <10 then encounter:=n.last;
end if;
for i in 1..encounter
loop
salesperson.extend(1);
salesperson(i):=n(i);
dbms_output.put_line('name of the employee:= '||salesperson(i).R_name||' name of
the job is ='||salesperson(i).R_job);
end loop;
end if;
end;
------------------------------------------------------------------
declare
TYPE Table_Type IS TABLE OF number;
t table_type:=table_type(12,4,5,2,3,12);
begin
t.delete(2);
if t.exists(1) then
dbms_output.put_line('The value is found in location 1');
end if;
if t.exists(2) then
dbms_output.put_line('the value in 2nd location found');
else
dbms_output.put_line('the value in 2nd location not found');
end if;
end;
--------------------------------------------------------------------------------
DECLARE
TYPE T_TABLE IS TABLE OF NUMBER;
T T_TABLE:=T_TABLE(12,4,32,1,6,5);
BEGIN
DBMS_OUTPUT.PUT_LINE('THE NUMBER OF ELEMENTES IN T_TABLE IS '||T.COUNT);
T.EXTEND(3);
DBMS_OUTPUT.PUT_LINE('THE NUMBER OF ELEMENTS IN T_TABLE IS '||T.COUNT);
T:=T_tABLE(2,4);
DBMS_OUTPUT.PUT_LINE('THE NUMBER OF ELEMENTS IN T_TABLE IS '||T.COUNT);
END;
set serveroutput on
-------------------------------------------------------------
declare
type v_array IS VARRAY(7) OF VARCHAR2(10);
V1 v_array:=v_array('swamy','rami','poomi','shami');
begin
dbms_output.put_line('the number of items in varray is '||v1.count);
dbms_output.put_line('the maximum number of items you can put in array v1 '||v1.
limit);
dbms_output.put_line('remaining number of items you can store '||to_Char(v1.lim
it-v1.count,'99'));
end;
------------------------------------------------------
declare
Type tt IS TABLE OF NUMBER;
TTT tt:=tt(1,21,13,24,25,46);
counter Number;
begin
ttt.delete(2);
dbms_output.put_line('total number of items in table is = '||ttt.count);
counter:=ttt.first;
while counter is not null
loop
dbms_output.put_line('the count number is '||counter||'and the value is '||ttt(c
ounter));
counter:=ttt.next(counter);
end loop;
counter:=ttt.last;
while counter is not null
loop
dbms_output.put_line('the count number is '||counter||' and the value is '||ttt
(counter));
counter:=ttt.prior(counter);
end loop;
end;
-------------------------------------------------------------------------