Вы находитесь на странице: 1из 4

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

Addition
------------------------------------------------------
declare
num1 number(20):=&v_num;
num2 number(20):=&s_num;
begin
/*num3:= num1+num2;
dbms_output.put_line('Result is: '||(num1++num2));
end;
/

-------------------------------------------------------
exception handling
------------------------------------------------------
declare
l_empid employees.employee_id%type:=&empid;
/*l_empsal employees.salary%type;*/
begin
begin
select salary into :h_num
from employees
where employee_id=l_empid;
exception
when others then
:h_num:=0;
dbms_output.put_line('Salary is: '||(:h_num));
end;
exception
when others then
dbms_output.put_line('Error!!');
end;
/
-------------------------------------------------------
nested block with labels
------------------------------------------------------
<<outer block>>
declare
l_empid employees.employee_id%type:=&empid;
/*l_empsal employees.salary%type;*/
begin
<<inner block>>
declare
l_empid employees.employee_id%type:=&empid;

begin
dbms_output.put_line(l_empid);
end;
begin
dbms_output.put_line(outer.l_empid);
end;
exception
when others then
dbms_output.put_line('Error!!');
end;
/
-------------------------------------------------------
insert statements
------------------------------------------------------
begin
insert into employee_data(ename,gender,dob)
values('Nihar','M',sysdate);
update employee_data
set ename='Shah Rukh'
where empid=1;
delete employee_data where
empid=3;
commit;
exception
when others then
rollback;
end;
-------------------------------------------------------
if then
------------------------------------------------------

DECLARE
v_num1 NUMBER := 5;
v_num2 NUMBER := 3;
v_temp NUMBER;
BEGIN
-- if v_num1 is greater than v_num2 rearrange their values
IF v_num1 > v_num2 THEN
v_temp := v_num1;
v_num1 := v_num2;
v_num2 := v_temp;
END IF;
-- display the values of v_num1 and v_num2
DBMS_OUTPUT.PUT_LINE ('v_num1 = '||v_num1);
DBMS_OUTPUT.PUT_LINE ('v_num2 = '||v_num2);
END;
/
-------------------------------------------------------
if-else if
------------------------------------------------------
DECLARE
l_studname varchar2(40):= '&name';
l_studdate date:= to_date('&b','DD-MM-YY');
l_marks NUMBER:= &marks;
l_grade varchar2(40):=null;
BEGIN
DBMS_OUTPUT.PUT_LINE ('The Student '||l_studname|| '''s grade is: ');
IF l_marks between 91 and 100 THEN
DBMS_OUTPUT.PUT_LINE ('E');
ELSIF l_marks between 81 and 90 THEN
DBMS_OUTPUT.PUT_LINE ('A+');
ELSIF l_marks between 71 and 80 THEN
DBMS_OUTPUT.PUT_LINE ('A');
ELSIF l_marks between 61 and 70 THEN
DBMS_OUTPUT.PUT_LINE ('B');
ELSIF l_marks between 51 and 60 THEN
DBMS_OUTPUT.PUT_LINE ('C');
ELSE DBMS_OUTPUT.PUT_LINE ('D');
END CASE;
END;
/
-------------------------------------------------------
case
------------------------------------------------------

DECLARE
l_studname varchar2(40):= '&name';
l_studdate date:= to_date('&b','DD-MM-YY');
l_marks NUMBER:= &marks;
l_grade varchar2(40):=null;
BEGIN
DBMS_OUTPUT.PUT_LINE ('The Student '||l_studname|| '''s grade is: ');
case
when l_marks between 91 and 100 THEN
DBMS_OUTPUT.PUT_LINE ('E');
when l_marks between 81 and 90 THEN
DBMS_OUTPUT.PUT_LINE ('A+');
when l_marks between 71 and 80 THEN
DBMS_OUTPUT.PUT_LINE ('A');
when l_marks between 61 and 70 THEN
DBMS_OUTPUT.PUT_LINE ('B');
when l_marks between 51 and 60 THEN
DBMS_OUTPUT.PUT_LINE ('C');
ELSE DBMS_OUTPUT.PUT_LINE ('D');
END CASE;
END;
/
-------------------------------------------------------
while
------------------------------------------------------
DECLARE
v_counter NUMBER := 4;
BEGIN
WHILE (v_counter < 5) LOOP
DBMS_OUTPUT.PUT_LINE (v_counter);
v_counter := v_counter - 1;
exit when v_counter=0;
END LOOP;
END;
/
-------------------------------------------------------
for
------------------------------------------------------

BEGIN
<<outer>>
for var in 1..10 LOOP
DBMS_OUTPUT.PUT_LINE (var);
for var1 in 1..10 LOOP
if var1=5 then
continue outer;
end if;
DBMS_OUTPUT.PUT_LINE (var1);
END LOOP;
END LOOP;
END;

-------------------------------------------------------
varray
------------------------------------------------------
declare
type t_empdata is varray(10) of number;
l_vaempdata t_empdata;
BEGIN
l_vaempdata:=t_empdata();
l_vaempdata.extend;
l_vaempdata(1):=101;
l_vaempdata.extend;
l_vaempdata(2):=102;
l_vaempdata.extend;
l_vaempdata(3):=103;
l_vaempdata.extend;
l_vaempdata(4):=104;
l_vaempdata.extend;
l_vaempdata(5):=105;
l_vaempdata.trim;
for var in 1..l_vaempdata.count loop
dbms_output.put_line(l_vaempdata(var));
end loop;
dbms_output.put_line(l_vaempdata.limit);
dbms_output.put_line(l_vaempdata.prior(l_vaempdata.last-1));
dbms_output.put_line(l_vaempdata.next(l_vaempdata.last-1));
END;
/