oracle_day3_
--3:使用PL/SQL增加部門信息(加入異常處理)
select * from dept;
declare
v_id dept.deptno%type:=&請輸入編號;
v_dname dept.dname%type:='&請輸入部門名稱';
v_loc dept.loc%type:='&請輸入辦公地點';
begin
insert into dept values(v_id,v_dname,v_loc);
commit;
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line('主鍵重複');
when others then
dbms_output.put_line('其他異常');
end;
select * from dept;
declare
v_id dept.deptno%type:=&請輸入編號;
v_dname dept.dname%type:='&請輸入部門名稱';
v_loc dept.loc%type:='&請輸入辦公地點';
del_inse exception;
pragma exception_init(del_inse,-1);
begin
insert into dept values(v_id,v_dname,v_loc);
commit;
exception
when del_inse then
dbms_output.put_line('主鍵重複惹');
when others then
dbms_output.put_line('其他異常');
end;
--4:購買商品時,如果庫存不足,則報異常(自定義異常)
select * from es_product
declare
v_id2 es_product.id%type:=&請輸入編號;
v_unm es_product.stockcount%type:=&請輸入購買數量;
va_stockcount es_product.stockcount%type;
kuncun exception;
begin
select stockcount into va_stockcount from es_product where id=v_id2;
if v_unm>va_stockcount then
raise kuncun;
end if;
update es_product set stockcount=va_stockcount-v_unm where id=v_id2;
commit;
exception
when kuncun then
dbms_output.put_line('庫存只有'||va_stockcount);
when others then
dbms_output.put_line('其他異常');
end;
--5:循環插入10個員工信息(名字可以爲 員工1,員工2)
使用隱式遊標判斷是否執行成功
declare
begin
for eno in 1..10 loop
INSERT INTO es_user VALUES(10||eno,'員工'||eno,'admin','張管','13801000100','北京市中關村1號','100100','[email protected]',0);
if sql%found then
dbms_output.put_line('執行成功');
else
dbms_output.put_line('執行bu成功');
end if;
commit;
end loop;
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line('主鍵重複了');
when others then
dbms_output.put_line('其他異常');
end;
--6:刪除用戶,如果違反完整性約束,使用非預定義異常
select * from es_user;
declare
v_id4 es_user.id%type:=&請輸入編號;
pk_del_exception exception;
pragma exception_init(pk_del_exception,-2292);
begin
delete from es_user where id=v_id4;
exception
when pk_del_exception then
dbms_output.put_line('違反完整性約束');
when others then
dbms_output.put_line('其他異常');
end;
--7:用數據泵到處scott下的員工表和部門表,然後再導入
create directory dupt_dir4 as 'D:\2017';
grant read,write on directory dupt_dir4 to scott;
expdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
tables=emp,dept;
impdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
tables=emp,dept;
drop table emp;
drop table dept;select * from emp
s
select * from dept;
declare
v_id dept.deptno%type:=&請輸入編號;
v_dname dept.dname%type:='&請輸入部門名稱';
v_loc dept.loc%type:='&請輸入辦公地點';
begin
insert into dept values(v_id,v_dname,v_loc);
commit;
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line('主鍵重複');
when others then
dbms_output.put_line('其他異常');
end;
select * from dept;
declare
v_id dept.deptno%type:=&請輸入編號;
v_dname dept.dname%type:='&請輸入部門名稱';
v_loc dept.loc%type:='&請輸入辦公地點';
del_inse exception;
pragma exception_init(del_inse,-1);
begin
insert into dept values(v_id,v_dname,v_loc);
commit;
exception
when del_inse then
dbms_output.put_line('主鍵重複惹');
when others then
dbms_output.put_line('其他異常');
end;
--4:購買商品時,如果庫存不足,則報異常(自定義異常)
select * from es_product
declare
v_id2 es_product.id%type:=&請輸入編號;
v_unm es_product.stockcount%type:=&請輸入購買數量;
va_stockcount es_product.stockcount%type;
kuncun exception;
begin
select stockcount into va_stockcount from es_product where id=v_id2;
if v_unm>va_stockcount then
raise kuncun;
end if;
update es_product set stockcount=va_stockcount-v_unm where id=v_id2;
commit;
exception
when kuncun then
dbms_output.put_line('庫存只有'||va_stockcount);
when others then
dbms_output.put_line('其他異常');
end;
--5:循環插入10個員工信息(名字可以爲 員工1,員工2)
使用隱式遊標判斷是否執行成功
declare
begin
for eno in 1..10 loop
INSERT INTO es_user VALUES(10||eno,'員工'||eno,'admin','張管','13801000100','北京市中關村1號','100100','[email protected]',0);
if sql%found then
dbms_output.put_line('執行成功');
else
dbms_output.put_line('執行bu成功');
end if;
commit;
end loop;
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line('主鍵重複了');
when others then
dbms_output.put_line('其他異常');
end;
--6:刪除用戶,如果違反完整性約束,使用非預定義異常
select * from es_user;
declare
v_id4 es_user.id%type:=&請輸入編號;
pk_del_exception exception;
pragma exception_init(pk_del_exception,-2292);
begin
delete from es_user where id=v_id4;
exception
when pk_del_exception then
dbms_output.put_line('違反完整性約束');
when others then
dbms_output.put_line('其他異常');
end;
--7:用數據泵到處scott下的員工表和部門表,然後再導入
create directory dupt_dir4 as 'D:\2017';
grant read,write on directory dupt_dir4 to scott;
expdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
tables=emp,dept;
impdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
tables=emp,dept;
drop table emp;
drop table dept;select * from emp
s
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.