Exceptions
Exceptions
Exceptions
a exception;
begin
if to_char(sysdate,'DY')='tue'
then
raise a;
end if;
exceptio
when a then
raise_application_error(-20345, 'My exception raised on tue');
end;
/
declare
a exception;
begin
if to_char(sysdate,'DY')='FRI' then
raise a;
end if;
exception
raise_exception_error(-20234,'My exception raised on Friday');
end;
/
begin
p1(7902);
end;
/
variable x number;
exec p1(7, :x);
print x;
declare
z number(10);
begin
p1(8,z);
dbms_output.put_line(z);
end;
variable a number;
exec p1(‘SMITH’, :a);
print a;
declare
x number(10):=&x;
begin
p1(x);
dbms_output.put_line(x);
end;
DECLARE
TOTAL_ROWS NUMBER(2);
BEGIN
UPDATE EMP
SET SAL = SAL + 5000;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('NO EMP UPDATED');
ELSIF SQL%FOUND THEN
TOTAL_ROWS:= SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(TOTAL_ROWS ||'EMP UPDATED');
END IF;
END;
BEGIN
UPDATE EMP SET DEPTNO=&DEPTNO
WHERE EMPNO=&EMPNO;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('EMPLOYEE DEPTNO SUCCESSFULLY UPDATED');
END IF;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('EMPLOYEE CAN NOT SUCCESSFULLY UPDATED');
END IF;
END;
DECLARE
ROWS_AFFECTED CHAR(4)
BEGIN
UPDATE ACCTMSTR SET STATUS='A' WHERE STATUS='S' AND BRANCHNUM IN (SELECT BRANCHNUM
FROM BRANCHMSTR WHERE BRANCHNAME='SBI');
ROWS_AFFECTED:=TO_CHAR SQL%ROWCOUNT;
IF SQL%ROWCOUNT > 0 THEN
DBMS_OUTPUT.PUT_LINE(ROWS_AFFECTED ||'ACCOUNTS ACTIVATED SUCESSFULLY');
ELSE
DBMS_OUTPUT.PUT_LINE('CURRENTLY THERE EXITS NO INACTIVE ACCOUNT IN
SBI:BRANCHNAME');
END IF;
END;
DECLARE
/*CREATE STRONG REF CURSOR POINTER TYPE */
TYPE MY_REFCUR IS REF CURSOR RETURN EMP % ROWTYPE;
/*CREATE CURSOR CURSORVARIABLE*/
CUR_VAR MY_REFCUR;
REC_VAR EMP % ROWTYPE;
begin
open
CUR_VAR for select * from emp where EMPNO=7566;
fetch CUR_VAR into REC_VAR;
dbms_output.put_line('EMP'|| REC_VAR.ENAME || 'ROW SALARY'|| REC_VAR.SAL);
end;
/
DECLARE
V1 SYS_REFCURSOR;
I EMP%ROWTYPE;
BEGIN
OPEN V1 FOR SELECT * FROM EMP;
LOOP
FETCH V1 INTO I;
EXIT WHEN V1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(I.ENAME||' '|| I.SAL);
END LOOP;
CLOSE V1;
END;
create table test as select rownum empno, ename from emp where rownum<=10;
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
declare
z varchar2(10);
begin
z:= f1('welcome');
dbms_output.put_line(z);
end;
/
declare
v_ename varchar2(10);
v_sal number(10);
begin
select ename, sal into v_ename, v_sal from emp where empno= &empno;
dbms_output.put_line(v_ename||' '||v_sal);
end;
declare
z varchar2(10);
begin
z := f1(8);
dbms_output.put_line(z);
end;
/
declare
x varchar2(10);
begin
x :=7 'abc';
dbms_output.put_line(x);
end;
desc user_sourse;
Sql> select text from user_source where name=’P1’;
select *from emp e1 where 2= (select count (distinct sal) from emp e2 where e2.
sal>=e1.sal);
SELECT * FROM EMP E1 WHERE 3= (SELECT COUNT (DISTINCT SAL) FROM EMP E3 WHERE
E3.SAL>=E1.SAL);
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
V_INFO EMP%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO V_INFO;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_INFO.EMPNO||' '||V_INFO.ENAME||' '||V_INFO.DEPTNO||' '||
V_INFO.JOB);
END LOOP;
CLOSE C1;
END;
DECLARE
C_EMPNO EMP.EMPNO%TYPE;
C_ENAME EMP.ENAME%TYPE;
C_JOB EMP.JOB%TYPE;
C_MGR EMP.MGR%TYPE;
C_HIREDATE EMP.HIREDATE%TYPE;
C_SAL EMP.SAL%TYPE;
C_COMM EMP.COMM%TYPE;
C_DEPTNO EMP.DEPTNO%TYPE;
CURSOR C_EMP IS SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO FROM EMP;
BEGIN
OPEN C_EMP;
LOOP
FETCH C_EMP INTO C_EMPNO,C_ENAME,C_JOB,C_MGR,C_HIREDATE,C_SAL,C_COMM,C_DEPTNO;
EXIT WHEN C_EMP%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(C_EMPNO||' '||C_ENAME||' '||C_JOB||' '||C_MGR||' '||
C_HIREDATE||' '||C_SAL||' '||C_COMM||' '||C_DEPTNO);
END LOOP;
CLOSE C_EMP;
END;
DECLARE
CURSOR GURU99_DET IS SELECT ENAME FROM EMP;
V_EMP_ENAME EMP.ENAME%TYPE;
BEGIN
OPEN GURU99_DET;
LOOP
FETCH GURU99_DET INTO V_EMP_ENAME;
IF GURU99_DET%NOTFOUND
THEN
EXIT;
END IF;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE E FETCHED:'||V_EMP_ENAME);
END LOOP;
DBMS_OUTPUT.PUT_LINE('TOTAL ROWS FETCHED IS '||GURU99_DET%ROWCOUNT);
CLOSE GURU99_DET;
END;
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
BEGIN
FOR I IN C1
LOOP
DBMS_OUTPUT.PUT_LINE(I.ENAME||''||I.SAL);
END LOOP;
END;
declare
type t1 is ref cursor;
v_t t1;
i emp%rowtype;
begin
open v_t for select * from emp where sal>2000;
loop
fetch v_t into i;
exit when v_t%notfound;
dbms_output.put_line(i.ename||' '||i.sal);
end loop;
close v_t;
end;
/
declare
v_ename varchar2(20);
v_sal number(10);
begin
select ename, sal into v_ename, v_sal from emp where empno= &no;
dbms_output.put_line(v_ename||''||v_sal);
exception
when no_data_found then
dbms_output.put_line('your employee does not exist’);
end;
/
declare
v_sal number(10);
begin
select sal into v_sal from emp;
dbms_output.put_line(v_sal);
exception
when too_many_rows then
dbms_output.put_line('not to return multiple rows');
end;
begin
insert into emp (empno)values(7902);
exception
when dup_val_on_index then
dbms_output.put_line('not to insert duplicates data');
end;
declare
cursor c1 is select * from emp;
i emp%rowtype;
begin
open c1;
loop
fetch c1 into i;
exit when c1%notfound;
dbms_output.put_line(i.ename||' '||i.sal);
end loop;
close c1;
exception
when invalid_cursor then
dbms_output.put_line('first we must open the cursor');
end;
declare
cursor c1 is select * from emp;
i emp%rowtype;
begin
open c1;
loop
fetch c1 into i;
exit when c1%notfound;
dbms_output.put_line(i.ename||' '||i.sal);
end loop;
open c1;
exception
when cursor_already_open then
dbms_output.put_line('first we must close the cursor before reopen the cursor');
end;
with
table1 as (select empno,ename,job from emp),
table2 as (select deptno, dname,loc from dept)
select ename, dname from table1 inner join table2 on (table1.empno = table2.deptno)
declare
type t1 is table of number(10)
index by binary_integer;
v_t t1;
begin
v_t(1):=10;
v_t(2):=20;
v_t(3):=30;
v_t(4):=40;
v_t(5):=50;
dbms_output.put_line(v_t(1));
dbms_output.put_line(v_t.first);
dbms_output.put_line(v_t.last);
dbms_output.put_line(v_t(4));
dbms_output.put_line(v_t.next(3));
v_t.delete(1,3);
dbms_output.put_line(v_t.count);
v_t.delete;
dbms_output.put_line(v_t.count);
end;
/
declare
type t1 is table of number(10) index by binary_integer;
v_t t1;
cursor c1 is select empno from emp;
m number(10) :=1;
begin
open c1;
loop
fetch c1 into v_t (m);
exit when c1%notfound;
m:=m+1;
end loop;
close c1;
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
end;
declare
type t1 is table of date index by binary_integer;
v_t t1;
begin
for i in 1..10
loop
v_t(i):=sysdate;
end loop;
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
end;
declare
type t1 is table of date index by binary_integer;
v_t t1;
begin
select hiredate bulk collect into v_t from emp;
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
end;
declare
type t1 is table of varchar2(20) index by binary_integer;
v_t t1;
begin
v_t(1):='murali';
v_t(2):='abc';
v_t(3):='xyz';
v_t(4):='pqr';
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
end;
declare
type t1 is table of number(10)
index by binary_integer;
v_t t1;
begin
v_t(500):=80;
dbms_output.put_line(v_t(500));
end;
declare
type t1 is table of number(10);
v_t t1:=t1();
begin
v_t(500):= 80;
dbms_output.put_line(v_t(500));
end;
declare
type t1 is table of number(10);
v_t t1:= t1();
begin
v_t.extend(500);
v_t(500):= 30;
dbms_output.put_line(v_t(500));
end;
declare
type t1 is table of number(10);
v_t t1:= t1();
begin
v_t(1):= 10;
v_t(2):= 20;
v_t(3):= 30;
v_t(4):= 40;
dbms_output.put_line(v_t(1));
end;
declare
type t1 is table of number(10);
v_t t1:= t1(10,20,30,40,50);
begin
dbms_output.put_line(v_t.count);
v_t.trim;
dbms_output.put_line(v_t.count);
v_t.delete(2);
for i in v_t.first..v_t.last
loop
if v_t.exists(i) then
dbms_output.put_line(v_t(i));
end if;
end loop;
end;
declare
type t1 is table of number(10);
v_t t1:= t1(10,20,30,40);
begin
v_t.trim(2);
dbms_output.put_line('after deleting last two elements');
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
v_t.delete(2);
dbms_output.put_line('after deleting last second elements');
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
v_t.delete(2);
dbms_output.put_line('after deleting last second elements');
for i in v_t.first..v_t.last
loop
if v_t.exists(i) then
dbms_output.put_line(v_t(i));
end if;
end loop;
end;
declare
type t1 is varray(10) of varchar2(10);
v_t t1:= t1();
begin
select ename bulk collect into v_t from emp where rownum<=10;
for i in v_t.first..v_t.last
loop
dbms_output.put_line(v_t(i));
end loop;
end;
declare
i number(10):= 1;
begin
loop
dbms_output.put_line(i);
exit when i>=10;
i:= i+1;
end loop;
end;
declare
var1 number;
var2 number;
begin
var1:=100;
var2:=1;
loop
dbms_output.put_line(var1*var2);
if(var2=10)then
exit;
end if;
var2:=var2+1;
end loop;
end;
declare
i integer:=1;
begin
while i<=10
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
declare
var1 number;
var2 number;
begin
var1:=200;
var2:=1;
while var2<10
loop
dbms_output.put_line(var1*var2);
var2:=var2+1;
end loop;
end;
Begin
for k in 1..10
loop
dbms_output.put_line(k);
end loop;
end
declare
var1 number;
begin
var1:=10;
for var2 in 1..10
loop
dbms_outpt.put_line(var1*var2);
end loop;
end;