在數據庫編程中,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'
