PL/SQL 检查查询是否返回空 [英] PL/SQL check if query returns empty
本文介绍了PL/SQL 检查查询是否返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序,我需要检查我的选择查询是否返回了空记录.(本例中是否没有x,y架子)
I'm writing a procedure, and i need to check whether my select query returned an empty record or not. (In this example whether there is no x,y shelf)
我该怎么做?
我试过了:
temp shelves.loadability%TYPE := NULL;
BEGIN
select loadability into temp from shelves where rownumber = x and columnnumber = y;
IF temp IS NOT NULL THEN
/* do something when it's not empty */
ELSE
/* do the other thing when it's empty */
END IF;
但是 if 的第二个分支永远不起作用...
But the second branch of the if never works...
哦,太简单了...
temp shelves.loadability%TYPE;
BEGIN
select count(*) into temp from shelves where rownumber = x and columnnumber = y;
IF temp != 0 THEN
/* do something when it's not empty */
ELSE
/* do the other thing when it's empty */
END IF;
END;
推荐答案
使用异常处理程序
Begin
select column
into variable
from table
where ...;
-- Do something with your variable
exception
when no_data_found then
-- Your query returned no rows --
when too_many_rows
-- Your query returned more than 1 row --
end;
这篇关于PL/SQL 检查查询是否返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文