Sunteți pe pagina 1din 8

chapter (1,2,3,4,8)

next (5,6,7,sg1 sp)


sp-store procuder
udf-user define function
packagesqlplsql-veribale is defind to exqcute the programme
veriable hold below charater in PLSQL
char & vaerchar2 (32767)
veriable can may be the boolean in PLSQL programm
scalar datatype
complosite datatype
record
collection
index by table
nested table
varray-

----------------------declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
dbms_output.put_line('n1 is' || n1 );
dbms_output.put_line('v1 is' || v1 );
end;
-------------------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
select ename,sal into v1,n1 from
dbms_output.put_line('n1 is ' ||
dbms_output.put_line('v1 is ' ||
end;
/
-----------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
select ename,sal into v1,n1 from

emp where empno=7788;


n1 );
v1 );

emp where empno=&empno;

dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
end;
/
-- it will ask to user or operator to input the value
----------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
select ename,sal into v1,n1 from emp where empno=&empno;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
end;
/
-- error handing
----------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
--select ename,sal into v1,n1 from emp where empno=&empno;
select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
end;
/
-- multipal row error handing
----------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
select ename,sal into n1,v1 from emp where empno=&empno;
--select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception

when no_data_found then


dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error then
dbms_output.put_line('Truncation or conversion error');
end;
/
-- character to number conversion error
----------------------set serveroutput on
declare
n1 number (6);
v1 varchar(30);
begin
n1:=10;
v1:='Excuting Pl Sql Block';
select ename,sal into v1,n1 from emp where empno='&empno';
--select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error or invalid_number then
dbms_output.put_line('Truncation or conversion error');
end;
/
-- character to number conversion error on sql statement execution case
----------------------set serveroutput on
declare
n1 number (6):=5;
v1 varchar(30);
n2 number(6):=0;
begin
v1:='Excuting Pl Sql Block';
n1:=n1/n2;
--select ename,sal into v1,n1 from emp where empno='&empno';
--select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error or invalid_number then
dbms_output.put_line('Truncation or conversion error');
when zero_divide then
dbms_output.put_line('denominator ca nit be zero');

end;
/
-----------------------set serveroutput on
declare
n1 number (6):=5;
v1 varchar(30);
n2 number(6):=0;
begin
--v1:='Excuting Pl Sql Block';
--n1:=n1/n2;
begin
insert into dept values (10, 'IT','MUM');
--exception
--when dup_val_on_index then
--dbms_output.put_line('-- inner department already exists');
end;
select ename,sal into v1,n1 from emp where empno='&empno';
--select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error or invalid_number then
dbms_output.put_line('Truncation or conversion error');
when zero_divide then
dbms_output.put_line('denominator ca nit be zero');
when dup_val_on_index then
dbms_output.put_line('--outer ');
end;
/
--for the duplicat data
-----------------------------------set serveroutput on
declare
n1 number (6):=5;
v1 varchar(30);
n2 number(6):=0;
begin
--v1:='Excuting Pl Sql Block';
--n1:=n1/n2;
begin
insert into dept values (10, 'IT','MUM');
--exception
--when dup_val_on_index then
--dbms_output.put_line('-- inner department already exists');
end;
select ename,sal into v1,n1 from emp where empno='&empno';
--select ename,sal into v1,n1 from emp where deptno=10;

dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error or invalid_number then
dbms_output.put_line('Truncation or conversion error');
when zero_divide then
dbms_output.put_line('denominator ca nit be zero');
--when dup_val_on_index then
--dbms_output.put_line('--outer ');
when others then
dbms_output.put_line(sqlcode || '---' || sqlerrm);
end;
/
-- it handle all error other then predefine
---------------------------create table error_log
(
uname varchar2(30),
errcode number(10),
errmesg varchar2(1000),
errdate timestamp
);
----------------------------set serveroutput on
declare
n1 number (6):=5;
v1 varchar(1000);
n2 number(6):=0;
begin
--v1:='Excuting Pl Sql Block';
--n1:=n1/n2;
begin
insert into dept values (10, 'IT','MUM');
--exception
--when dup_val_on_index then
--dbms_output.put_line('-- inner department already exists');
end;
select ename,sal into v1,n1 from emp where empno='&empno';
--select ename,sal into v1,n1 from emp where deptno=10;
dbms_output.put_line('n1 is ' || n1 );
dbms_output.put_line('v1 is ' || v1 );
exception
when no_data_found then
dbms_output.put_line('invalid emp no');
when too_many_rows then
dbms_output.put_line('multipal rows');
when value_error or invalid_number then
dbms_output.put_line('Truncation or conversion error');
when zero_divide then
dbms_output.put_line('denominator ca nit be zero');
--when dup_val_on_index then
--dbms_output.put_line('--outer ');
n1:=sqlcode;

v1:=sqlerrm;
when others then
insert into error_log values(user,n1,v1,localtimestamp);
--dbms_output.put_line(sqlcode || '---' || sqlerrm);
end;
/
-- insert the error into table
-------------------------------------(locking)
DECLARE
qty_on_hand NUMBER(5);
BEGIN
SELECT quantity INTO qty_on_hand FROM inventory
WHERE product = 'TENNIS RACKET'
FOR UPDATE OF quantity nowait;
IF qty_on_hand > 0 THEN -- check quantity
UPDATE inventory SET quantity = quantity - 1
WHERE product = 'TENNIS RACKET';
INSERT INTO purchase_record
VALUES ('Tennis racket purchased', SYSDATE);
ELSE
INSERT INTO purchase_record
VALUES ('Out of tennis rackets', SYSDATE);
END IF;
-- COMMIT;
END;
/
--------------------------------------------DECLARE
qty_on_hand NUMBER(5);
BEGIN
SELECT quantity INTO qty_on_hand FROM inventory
WHERE product = 'TENNIS RACKET'
FOR UPDATE OF quantity nowait;
IF qty_on_hand > 0 THEN -- check quantity
UPDATE inventory SET quantity = quantity - 1
WHERE product = 'TENNIS RACKET';
INSERT INTO purchase_record
VALUES ('Tennis racket purchased', SYSDATE);
ELSE
INSERT INTO purchase_record
VALUES ('Out of tennis rackets', SYSDATE);
END IF;
-- COMMIT;
END;
/
-- for no waiting for the user bt no transaction us done
--------------------------------------------set serveroutput on
DECLARE
qty_on_hand NUMBER(5);

row_lock exception;
pragma exception_init (row_lock,-54);
BEGIN
SELECT quantity INTO qty_on_hand FROM inventory
WHERE product = 'TENNIS RACKET'
FOR UPDATE OF quantity nowait;
IF qty_on_hand > 0 THEN -- check quantity
UPDATE inventory SET quantity = quantity - 1
WHERE product = 'TENNIS RACKET';
INSERT INTO purchase_record
VALUES ('Tennis racket purchased', SYSDATE);
ELSE
INSERT INTO purchase_record
VALUES ('Out of tennis rackets', SYSDATE);
END IF;
exception
when row_lock then
dbms_output.put_line('please try after some time');
-- COMMIT;
END;
/
-- for no waiting for the user bt no transaction us done
--------------------------------------------set serveroutput on
DECLARE
qty_on_hand NUMBER(5);
row_lock exception;
pragma exception_init (row_lock,-54);
BEGIN
SELECT quantity INTO qty_on_hand FROM inventory
WHERE product = 'TENNIS RACKET'
FOR UPDATE OF quantity wait 5;
IF qty_on_hand > 0 THEN -- check quantity
UPDATE inventory SET quantity = quantity - 1
WHERE product = 'TENNIS RACKET';
INSERT INTO purchase_record
VALUES ('Tennis racket purchased', SYSDATE);
ELSE
INSERT INTO purchase_record
VALUES ('Out of tennis rackets', SYSDATE);
END IF;
exception
when row_lock then
dbms_output.put_line('please try after some time');
-- COMMIT;
END;
/
-- for no waiting for the user for 5 sec.
--------------------------------------------==========================================
to run the plsql
set serveroutput on
------

bullting exception or predefine exception


no_data_found (for the no record found handling)
too_many_rows (for the too many row in the out put found handling)
value_error (character to number conversion error)
invalid_number (character to number conversion error on sql statement execution)
zero_divide
dup_val_on_index
others for all the error catch by the system other then above predefine function
update inventory set quantity = 3 where prod_id = 1234;

S-ar putea să vă placă și