select into时no_date_found 导致程序中断的解决办法

我们知道在pl/sql中要想从数据表中向变量赋值,需要使用select into 子句。

但是它会带动来一些问题,如果查询没有记录时,会抛出no_data_found异常。

如果有多条记录时,会抛出too_many_rows异常。

这个是比较糟糕的。一旦抛出了异常,就会让过程中断。特别是no_data_found这种异常,没有严重到要让程序中断的地步,可以完全交给由程序进行处理。

CREATE OR REPLACE PROCEDURE procexception (
    p varchar2
)
AS
    v_postype varchar2(20);
BEGIN
    SELECT pos_type
    INTO v_postype
    FROM pos_type_tb1
    WHERE 1 = 0;--绝壁为空,报no_data_found的错误
    dbms_output.put_line(v_postype);
END;

1. 直接加上异常处理。

CREATE OR REPLACE PROCEDURE procexception (
    p varchar2
)
AS
    v_postype varchar2(20);
BEGIN
    SELECT pos_type
    INTO v_postype
    FROM pos_type_tb1
    WHERE 1 = 0;
    dbms_output.put_line(v_postype);
EXCEPTION
    WHEN no_data_found THEN dbms_output.put_line("未找到数据");

程序一就会被中断
2. select into做为一个独立的块,在这个块中进行异常处理

create or replace procedure procexception(
p varchar2
) as
  v_postype varchar2(20);
begin
  select pos_type into v_postype from pos_type_tb1 where 1=0;--绝壁为空
  dbms_output.put_line(v_postype);
--其实吧,我觉得和上面那种差不多,原理一样的呀,no zuo nod ie
exception
  when no_data_found then
    v_postype := '';
  end;
  dbms_output.put_line(v_postype);
end;

这是一种比较好的处理方式了。不会因为这个异常而引起程序中断。
3.使用游标

create or replace procedure proceexception(
p varchar2
) as
  v_postype varchar2(20);
  cursor c_postype is select pos_type from pos_type_tb1 where 1=0;
beign
  open c_postype;
  fetch c_postype into v_postype;
  close c_postype;
  dbms_output.put_line(v_postype);
end;
--个人觉得这样先进行非空判断会更好
create or replace procedure proceexception(
p varchar2
) as
  v_postype varchar2(20);
  cursor c_postype is select pos_type from pos_type_tb1 where 1=0;
beign
  if c_postype%found then
    open c_postype;
    fetch c_postype into v_postype;
    close c_postype;
    dbms_output.put_line(v_postype);
  end if;
end;

这样就完全的避免了no_data_found异常。完全交由程序员来进行控制了。

第二种情况是too_many_rows 异常的问题。

Too_many_rows 这个问题比起no_data_found要复杂一些。

给一个变量赋值时,但是查询结果有多个记录。

处理这种问题也有两种情况:

1. 多条数据是可以接受的,也就是说从结果集中随便取一个值就行。这种情况应该很极端了吧,如果出现这种情况,也说明了程序的严谨性存在问题。

2. 多条数据是不可以被接受的,在这种情况肯定是程序的逻辑出了问题,也说是说原来根本就不会想到它会产生多条记录。

对于第一种情况,就必须采用游标来处理,而对于第二种情况就必须使用内部块来处理,重新抛出异常。

多条数据可以接受,随便取一条,这个跟no_data_found的处理方式一样,使用游标。

我这里仅说第二种情况,不可接受多条数据,但是不要忘了处理no_data_found哦。这就不能使用游标了,必须使用内部块。

create or replace proceduer procexception2(
p varchar2
) as
v_postype varchar2(20);
begin
  begin
    select pos_type into v_postype from pos_type_tb1 where rownum < 5;
  exception
    when no_data_found then
      v_postype := null;
    when too_many_rows then
      raise_application_error(-20000,'对v_postype赋值是,找到多条数据');
  end;
  dbms_output.put_line(v_postype);
end;

需要注意的是一定要加上对no_data_found的处理,对出现多条记录的情况则继续抛出异常,让上一层来处理。

总之对于select into的语句需要注意这两种情况了。需要妥当处理啊。

在mysql数据里有一张表,建表语句如下: ``` CREATE TABLE `audit_bin_info` ( `BIN_PID` int(8) NOT NULL, `HOST_NAME` varchar(100) DEFAULT NULL , `SOCK_ID` int(8) DEFAULT NULL , `BIN_STS` tinyint(2) DEFAULT NULL , `BOOT_NAME` varchar(100) DEFAULT NULL, `CHANNEL_ID` tinyint(2) DEFAULT NULL , `START_DATE` datetime DEFAULT NULL , `UPDATE_DATE` datetime DEFAULT NULL, `MODULE_NAME` varchar(100) DEFAULT NULL, `BUSI_CONTENT` varchar(4000) DEFAULT NULL, `TASK_STS` smallint(4) DEFAULT NULL , `ID` bigint(15) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`ID`) USING BTREE, KEY `IDX_BIN_INFO` (`BOOT_NAME`,`MODULE_NAME`,`CHANNEL_ID`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=16766000 DEFAULT CHARSET=utf8 ``` 帮我写一个c++的函数,函数定义如下: ```void CMonitorBase::binlogToDb( const AISTD string & strBinName, const AISTD string & strBinType, const CClientList & listClient, const AISTD string &m_strChannelId, otl_connect& ocDbConn) ``` 它需求实现以下功能: 1、使用otl_stream查询audit_bin_info表,查询语句为```select id, bin_pid, host_name, sock_id from audit_bin_info where boot_name='"+strBinName+"' and module_name='"+strBinType+"' and channel_id = " + m_strChannelId```;将查到数据保存在一个vector结构中; 2、将查到的表数据跟listClient中的数据做比较,比较条件为表数据中的bin_pid, host_name, sock_id分别和CClient结构中的m_iAppId,m_strHostName,m_iSockId,都相等,则认为找到数据。 3、如果在listClient中找到相等的数据,则根据找到的数据update表中的数据,需要更新的字段为BIN_STS, START_DATE, BUSI_CONTENT,TASK_STS,UPDATE_DATE,前4个字段对别对应CClient结构中的m_nClientSts,dtmBoot,m_strBusiContent,m_nTaskSts,UPDATE_DATE取系统间; 如果在listClient中没找到相等的数据,则根据id值删除audit_bin_info表中的数据; 最后如果是listClient中多出来的数据,需要插入到audit_bin_info表中; 其中CClient和CClientList的定义如下: ``` class CClient { public: long m_idx; int32 m_iSockId; int32 m_iAppId; int64 m_llTaskId; int16 m_nTaskSts; int16 m_nClientSts; int16 m_nMaxTask; int16 m_nChannelId; AISTD string m_strBusiContent; AISTD string m_strHostName; INT64LIST m_listDetail; AISTD string m_strSpecSts; CBSDateTime dtmBoot; AISTD string m_strRetMsg; int16 m_nStatus; int16 m_nDispEsc; CClient() : m_idx(0), m_iSockId(0), m_iAppId(0), m_llTaskId(0), m_nTaskSts(0), m_nClientSts(1), m_nMaxTask(1), m_nChannelId(0), m_strSpecSts("0"), m_nStatus(0), m_nDispEsc(0) { dtmBoot = CBSDateTime::currentDateTime(); }; }; typedef AISTD vector<CClient*> CClientList; ```
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值