架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8311|回复: 0

[资料] oracle系统预定义异常和用户自定义异常

[复制链接]
发表于 2015-3-19 09:28:41 | 显示全部楼层 |阅读模式
create table stuinfo1
(
       stuid int primary key,
       stuname varchar(10) not null,
       stusex char(2) check(stusex='男' or stusex='女'),
       stubir date
);
insert into stuinfo values ('001','张三','男',to_date('1999-1-1','yyyy-mm-dd'));
insert into stuinfo values ('002','里斯','男',to_date('1997-12-1','yyyy-mm-dd'));
insert into stuinfo values ('003','王五','女',to_date('1995-3-4','yyyy-mm-dd'));
insert into stuinfo values ('004','哈哈','男',to_date('2000-7-5','yyyy-mm-dd'));
insert into stuinfo values ('005','哈哈','男',to_date('1993-7-5','yyyy-mm-dd'));


--系统预定义异常
declare
    v_name stuinfo.stuname%type;
begin
    select stuname into v_name from stuinfo;
--exception
--    when too_many_rows then
--         dbms_output.put_line('查询的结果多余一条,不能赋值');
end;
--用户自定义异常
declare
    money_error exception;
    money1 int := 1000;
    money2 int := &转账金额;
begin
    if money1-money2 < 0 then
       raise_application_error(-20001,'余额不足');
       --raise money_error;
    else
       dbms_output.put_line('转账成功');
    end if;
--exception
--    when money_error then
--         dbms_output.put_line('余额不足');
end;

--非预定义异常
declare
    myerror exception;
    pragma exception_init(myerror,-1);
begin
    insert into stuinfo values ('001','aa','女',to_date('1995-3-4','yyyy-mm-dd'));
exception
    when myerror then
        dbms_output.put_line('违反主键唯一约束');
end;

insert into stuinfo1 values (1,'sdf','男','');
select * from tb1;
drop table tb1;
--动态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;
--隐式游标
begin
        DBMS_OUTPUT.PUT_LINE('插入前:'||SQL%ROWCOUNT);
        update stuinfo set stusex = '男';
        DBMS_OUTPUT.PUT_LINE('插入后:'||SQL%ROWCOUNT);
END;






上一篇:oracle获取字符串长度函数length()和hengthb()
下一篇:RedhatEnterpriseserver6.3配置VPN
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-21 00:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表