Exceptions

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 18

declare

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;
/

create or replace procedure procedurename(formal parameters)


is/as
variable declarations, cursor, user defined exceptions;
begin
[exception]
end[procedur ename];

create or replace procedure p1(p_empno number)


is
v_ename varchar2(10);
v_sal number(10);
begin
select ename, sal into v_ename, v_sal from emp where empno= p_empno;
dbms_output.put_line(v_ename||' '||v_sal);
end;

begin
p1(7902);
end;
/

select text from user_source where name='P1';

create or replace procedure p1(p_deptno number)


is
cursor c1 is select * from emp where deptno=p_deptno;
i emp%rowtype;
begin
open c1;
loop
fetch c1 into I;
exit when c1%notfound;
dbms_output.put_line(i.ename||' '||i.sal||' '||i.deptno);
end loop;
close c1;
end;
/

create or replace procedure p1(p_deptno in number, p_dname in varchar2, p_loc in


varchar2)
is
begin
insert into dept values(p_deptno,p_dname,p_loc);
dbms_output.put_line('u r record inserted through procedures');
end;

variable x number;
exec p1(7, :x);
print x;

declare
z number(10);
begin
p1(8,z);
dbms_output.put_line(z);
end;

create or replace procedure p1(p_ename in varchar2, p_sal out nocopy number)


is
begin
select sal into p_sal from emp where ename=p_ename;
end;
/

variable a number;
exec p1(‘SMITH’, :a);
print a;

create or replace procedure p1(a in out number)


is
begin
a := a*a;
end;
/
variable z number;
exec :z:=8;
exec p1(:z);
print z;

declare
x number(10):=&x;
begin
p1(x);
dbms_output.put_line(x);
end;

create or replace procedure p1(p_x in out number)


as
begin
select sal into p_x from emp where empno=p_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;

create or replace function f1(a varchar2)


return varchar2
is
begin
return a;
end;
/

select f1('hi') from dual;

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;

create or replace function f1(a number)


return varchar2
is
begin
if mod(a,2)=0 then
return 'even';
else
return 'odd';
end if;
end;

declare
z varchar2(10);
begin
z := f1(8);
dbms_output.put_line(z);
end;
/

create or replace function f1


return number
is
v_sal number(10);
begin
select max(sal) into v_sal from emp;
return v_sal;
end;
/

create or replace function f1(p_ename varchar2)


return varchar2
is
v_job varchar2(10);
begin
select job into v_job from emp where ename= p_ename;
return v_job;
end;
/

declare
x varchar2(10);
begin
x :=7 'abc';
dbms_output.put_line(x);
end;

create or replace function f1(p_empno number, p_date date)


return number
is
x number(10);
begin
select months_between(p_date, hiredate)/12 into x from emp where empno=p_empno;
return round(x);
end;

create or replace function tax(p_empno number)


return number
is
v_sal number(10);
annsal number(10);
z number(10);
begin
select sal into v_sal from emp where empno= p_empno;
annsal := v_sal*12;
if annsal>1000 and annsal<=15000 then
z := annsal*0.1;
elsif annsal>20000 then
z := annsal*0.3;
else
z :=0;
end if;
return z;
end;

create or replace function f1(p_empno number)


return number
is
v_count number(10);
begin
delete from emp where empno= p_empno;
v_count:= sql%rowcount;
return v_count;
end;
declare
a number(10);
begin
a:= f1(1);
dbms_output.put_line(a);
end;

create or replace procedure p1(p_empno number)


is
v_ename varchar2(10);
v_sal number(10);
begin
select ename, sal into v_ename, v_sal from emp where empno= p_empno;
dbms_output.put_line(v_ename||' '||v_sal);
end;

desc user_sourse;
Sql> select text from user_source where name=’P1’;

create or replace procedure p1(p_deptno number)


is
cursor c1 is select * from emp where deptno=p_deptno;
i emp%rowtype;
begin
open c1;
loop
fetch c1 into I;
exit when c1%notfound;
dbms_output.put_line(i.ename||' '||i.sal||' '||i.deptno);
end loop;
close c1;
end;

create or replace procedure p1(p_deptno in number, p_dname in varchar2, p_loc in


varchar2)
is
begin
insert into dept values(p_deptno,p_dname,p_loc);
dbms_output.put_line('Four records is inserted');
end;
/
Output: sql> exec p1(1,’a’,’b’);

create or replace procedure p1(p_ename in varchar2, p_sal out number)


is
begin
select sal into p_sal from emp where ename=p_ename;
end;
/

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;

select level from dual connect by level <=10;

CREATE TABLE Sales.Staff (


StaffId int IDENTITY,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Email varchar(255) NOT NULL,
Phone varchar(25) NULL,
Active tinyint NOT NULL,
StoreId int NOT NULL,
ManagerId int NULL,
PRIMARY KEY CLUSTERED (StaffId),
UNIQUE (Email)
)
ON [PRIMARY]
GO

ALTER TABLE Sales.Staff


ADD FOREIGN KEY (ManagerId) REFERENCES Sales.Staff (StaffId)
GO

CREATE TABLE Albums


(AlbumId int (1,1) NOT NULL PRIMARY KEY,
AlbumName nvarchar(255) NOT NULL,
ReleaseDate date NOT NULL,
ArtistId int NOT NULL
REFERENCES Artists(ArtistId),
GenreId int NOT NULL);

create table examfee(sno number(10), Hallticket_no varchar2(10), Student_name


varchar2(10), examfee_paid_date date, amount number(10) );

sno Hallticket_no Student_name examfee_paid_date amount


1 18KA1A0201 Ramesh 11/04/2022 715
2 18KA1A0202 Suresh 12/05/2022 715
3 18KA1A0203 Raghava 11/04/2022 715
4 18KA1A0204 Ramya 09/04/2022 715
5 18KA1A0205 Sangetha 06/04/2022 715
6 18KA1A0206 Kiran 11/04/2022 715
7 18KA1A0207 Prem 12/04/2022 715

create table raj( empno number(4,0), ename varchar2(10), job


varchar2(9), mgr number(4,0), hiredate date, sal number(7,2),
comm number(7,2), deptno number(2,0));
create or replace package pj1
is
procedure p1;
procedure p2;
end;

create or replace package body pj1


is
procedure p1
is
begin
dbms_output.put_line('First Procedure');
end p1;
procedure p2
is
begin
dbms_output.put_line('Second Procedure');
end p2;
end;

create or replace package body pj1


is
procedure p1(A number, B number)
is
c number(10);
begin
c:=a+b;
dbms_output.put_line(c);
end p1;
procedure p1

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;

create or replace trigger th1 after update on emp


for each row
begin
dbms_output.put_line('row level');
end;
/

update emp set sal=sal+100 where deptno=30;

create table test(sno number(10), name varchar2(10), sal number(10))


partition by range(sal)(partition p1 values less than(1000),
partition p2 values less than(2000),
partition p3 values less than(3000),
partition p4 values less than(Maxvalue));

CREATE TABLE TEST(SNO NUMBER(10), NAME VARCHAR2(10), SAL NUMBER(10))


PARTITION BY RANGE(SAL)(PARTITION P1 VALUES LESS THAN(1000),
PARTITION P2 VALUES LESS THAN(2000),
PARTITION P3 VALUES LESS THAN(MAXVALUE));

CREATE GLOBAL TEMPORARY TABLE TEMP1


(ID INT, DESCRIPTION VARCHAR2(100)) ON COMMIT DELETE ROWS;

INSERT INTO TEMP1(ID, DESCRIPTION ) VALUES (1,'TRANSACTION SPECIFIC GLOBAL TEMP


TABLE');

CREATE TABLE sales_range


(salesman_id NUMBER(5),
salesman_name VARCHAR2(30),
sales_amount NUMBER(10),
sales_date DATE)
PARTITION BY RANGE(sales_date)
(PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','MM/DD/YYYY')),
PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','MM/DD/YYYY')),
PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','MM/DD/YYYY')),
PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','MM/DD/YYYY')));

CREATE OR REPLACE TRIGGER TR1


AFTER UPDATE ON EMP
BEGIN
DBMS_OUTPUT.PUT_LINE ('STATEMENT LEVEL');
END;

UPDATE EMP SET SAL=SAL+100 WHERE DEPTNO=30;

CREATE OR REPLACE TRIGGER TH1


AFTER UPDATE ON EMP
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE ('ROW LEVEL');
END;
INSERT INTO TH1 VALUES (1,XYS,2000);

create or replace function Tax(p_empno number)


return number
is
v_sal number(10);
annsal number(10);
z number(10);
begin
select sal into v_sal from emp where empno= p_empno;
annsal:= v_sal*12;
if annsal>10000 and annsal<=15000 then
z := annsal*0.1;
elsif annsal>=20000 then
Z := annsal*0.2;
else
Z := 0;
end if;
return Z;
end;

create or replace function f1(p_empno number, p_date date)


return number
is
x number(10);
begin
select months_between(p_date, hiredate)/12 into x from emp where empno=p_empno;
return round(x);
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;

You might also like