gpt4 book ai didi

plsql - PL/SQL 检查查询是否返回空

转载 作者:行者123 更新时间:2023-12-03 12:16:01 25 4
gpt4 key购买 nike

我正在编写一个过程,我需要检查我的选择查询是否返回空记录。
(在这个例子中是否没有 x,y 架子)

我怎样才能做到这一点?

我试过这个:

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 的第二个分支永远不起作用......

编辑:

哦,原来如此简单...
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;

关于plsql - PL/SQL 检查查询是否返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5955025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com