Oracle:select into 查询没有记录的解决办法

 在数据库编程中,select into 语句可以将数据库的某些值赋值给程序的变量,使用起来非常方便。但很多时候也会遇到查询出来没有记录的情况,这时程序会出错。

可以使用

exception
when NO_DATA_FOUND then

但是如merge into using 查不到时可以使用该方法;

merge into  web_user_vip wv using (select vip_type,id from(
      select nvl(max(vip_type),0)vip_type ,nvl(max(id),0)id from web_user_vip where user_id = v_user_id and source = 2 and status =1 and 
      vip_type = v_vip_type)where rownum = 1)e on (e.vip_type = wv.vip_type)
      when matched then
      UPDATE  SET wv.expire_time = v_expire_end_date where wv.id = e.id
      when not matched then
      insert(id,user_id,vip_type,expire_time,create_time,source,enterprise_id,status)
      VALUES(xshtest.WEB_USER_VIP_SEQ.NEXTVAL,v_user_id,v_vip_type,v_expire_end_date,sysdate,2,v_enterprise_id,1);

看下面示例:

select 没有数据的时候:

select t.prj_type_id into :id from PRJ_C_TYPE t where t.prj_type_name='abc'

使用聚合函数后:

select nvl(max(t.prj_type_id),0) into :id from PRJ_C_TYPE t where t.prj_type_name='abc'


原文地址:https://www.cnblogs.com/SimonHu1993/p/10197203.html