This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12545|Reply: 0

[Source] Oracle system predefined exceptions and user-defined exceptions

[Copy link]
Posted on 3/19/2015 9:28:41 AM | | |
create table stuinfo1
(
       stuid int primary key,
       stuname varchar(10) not null,
       stusex char(2) check(stusex='male' or stusex='female'),
       stubir date
);
insert into stuinfo values ('001','Zhang San','male',to_date ('1999-1-1','yyyy-mm-dd'));
insert into stuinfo values ('002','Reese','male',to_date ('1997-12-1','yyyy-mm-dd'));
insert into stuinfo values ('003', 'Wang Wu', 'female', to_date ('1995-3-4', 'yyyy-mm-dd'));
insert into stuinfo values ('004','haha','male',to_date ('2000-7-5','yyyy-mm-dd'));
insert into stuinfo values ('005', 'haha', 'male', to_date ('1993-7-5', 'yyyy-mm-dd'));


--System predefined exceptions
declare
    v_name stuinfo.stuname%type;
begin
    select stuname into v_name from stuinfo;
--exception
--    when too_many_rows then
-- dbms_output.put_line('Query results are redundant, cannot be assigned');
end;
--user-defined exceptions
declare
    money_error exception;
    money1 int := 1000;
    money2 int :=& transfer amount;
begin
    if money1-money2 < 0 then
       raise_application_error (-20001, 'Insufficient balance');
       --raise money_error;
    else
       dbms_output.put_line('Transfer successful');
    end if;
--exception
--    when money_error then
-- dbms_output.put_line('Insufficient balance');
end;

--non-predefined exceptions
declare
    myerror exception;
    pragma exception_init(myerror,-1);
begin
    insert into stuinfo values ('001','aa','female',to_date('1995-3-4','yyyy-mm-dd'));
exception
    when myerror then
        dbms_output.put_line('Violation of primary key uniqueness constraint');
end;

insert into stuinfo1 values (1,'sdf','male','');
select * from tb1;
drop table tb1;
--dynamic sql
begin
     execute immediate 'create table tb1(tid int)';
end;

declare
        sqlString varchar2(200);
        tId int:=2;
begin
        sqlString:='insert into tb1 values (:1)';

        execute immediate sqlString using tId;

end;


select * from stuinfo;
--Implicit cursor
begin
        DBMS_OUTPUT. PUT_LINE('Before inserting:'|| SQL%ROWCOUNT);
        update stuinfo set stusex = 'male';
        DBMS_OUTPUT. PUT_LINE('After inserting:'|| SQL%ROWCOUNT);
END;






Previous:oracle gets the string length functions length() and hengthb()
Next:Configure the VPN in Redhat Enterprise Server 6.3
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com