Plsql 在捕获的异常中使用光标执行选择操作
我正在尝试编写一个存储过程,它接受一个ID的输入,然后使用不同的条件在3个不同的表中搜索该项。我的一个解决方案是对每个表逐个执行select语句,如果通过Plsql 在捕获的异常中使用光标执行选择操作,plsql,Plsql,我正在尝试编写一个存储过程,它接受一个ID的输入,然后使用不同的条件在3个不同的表中搜索该项。我的一个解决方案是对每个表逐个执行select语句,如果通过select未找到任何数据,则捕获NO\u DATA\u FOUND异常 在伪代码中: Select item from from first table If no data found, throw exception. Handle exception by selecting data from second t
select
未找到任何数据,则捕获NO\u DATA\u FOUND
异常
在伪代码中:
Select item from from first table
If no data found, throw exception.
Handle exception by selecting data from second table
If no data found, throw another exception.
Handle exception by selecting data from third table (if the data is not present in any row it should return 0 rows)
以下是我所拥有的:
OPEN REQUEST FOR
SELECT REQ_TYPE, REQ_TYPE_STATUS FROM TABLEONE
WHERE TABLEONE.REQ_ID = REQUESTID
AND (REQ_STATUS = 'D' OR REQ_STATUS = 'A');
EXCEPTION WHEN NO_DATA_FOUND THEN
BEGIN
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' AS REQ_TYPE_STATUS FROM TABLETWO
WHERE TABLETWO.REQ_ID = REQUESTID;
EXCEPTION WHEN NO_DATA_FOUND THEN
begin
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' as REQ_TYPE_STATUS FROM THIRDTABLE
WHERE THIRDTABLE.REQ_ID = REQUESTID;
end;
END;
如果在TABLEONE中找到一个项目,它将成功返回该项目的数据。但是,在捕获的异常中执行的SELECT
操作似乎不会运行,因为存储过程不会返回任何行
我已经分别验证了我正在搜索的数据确实存在于表二和/或表三中
该语法在编译时是有效的,只是如果该项不存在于TABLEONE中(但存在于TABLETWO或TABLETWO中),它不会返回任何行
有什么想法吗 这要归功于@Franek 参考文件解释了以下内容: 循环光标是智能的。它不会像现在这样引发无数据发现;如果 没有要提取的内容,它将退出循环并终止 执行成功。因此,如果你想把它当作一个 例外-你不能 我试图做的显然是不可能的。因此,我以这种方式实施: 伪代码:
Search for the item in the first table
If item was found
select the details of it from the first table
If item was not found
search for the item in the second table
If Item was found
Select the details of it from the second table
If item was not found
Select the item from the third table (returning blank if it's not found here neither)
-- Search for the request ID in the first table
SELECT COUNT(REQ_ID) INTO requestFound FROM FIRSTTABLE
WHERE FIRSTTABLE.REQ_ID = REQUESTID
AND (REQ_STATUS = 'D' OR REQ_STATUS = 'A');
IF(REQUESTFOUND > 0) THEN
-- Select the request details
OPEN REQUEST FOR
SELECT REQ_ID, REQ_TYPE_STATUS FROM FIRSTTABLE
WHERE FIRSTTABLE.REQ_ID = REQUESTID
AND (REQ_STATUS = 'D' OR REQ_STATUS = 'A');
ELSE
-- Search for the request from the second table
SELECT COUNT(REQ_ID) INTO REQUESTFOUND FROM SECONDTABLE
WHERE SECONDTABLE.REQ_ID = REQUESTID;
IF(REQUESTFOUND > 0) THEN
-- Select the request details from second table
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' AS REQ_TYPE_STATUS FROM SECONDTABLE
WHERE SECONDTABLE.REQ_ID = REQUESTID;
ELSE
-- Get the request from third table (will return as blank if nothing found)
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' AS REQ_TYPE_STATUS FROM THIRDTABLE
WHERE THIRDTABLE.REQ_ID = REQUESTID;
END IF;
END IF;
PL/SQL实现:
Search for the item in the first table
If item was found
select the details of it from the first table
If item was not found
search for the item in the second table
If Item was found
Select the details of it from the second table
If item was not found
Select the item from the third table (returning blank if it's not found here neither)
-- Search for the request ID in the first table
SELECT COUNT(REQ_ID) INTO requestFound FROM FIRSTTABLE
WHERE FIRSTTABLE.REQ_ID = REQUESTID
AND (REQ_STATUS = 'D' OR REQ_STATUS = 'A');
IF(REQUESTFOUND > 0) THEN
-- Select the request details
OPEN REQUEST FOR
SELECT REQ_ID, REQ_TYPE_STATUS FROM FIRSTTABLE
WHERE FIRSTTABLE.REQ_ID = REQUESTID
AND (REQ_STATUS = 'D' OR REQ_STATUS = 'A');
ELSE
-- Search for the request from the second table
SELECT COUNT(REQ_ID) INTO REQUESTFOUND FROM SECONDTABLE
WHERE SECONDTABLE.REQ_ID = REQUESTID;
IF(REQUESTFOUND > 0) THEN
-- Select the request details from second table
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' AS REQ_TYPE_STATUS FROM SECONDTABLE
WHERE SECONDTABLE.REQ_ID = REQUESTID;
ELSE
-- Get the request from third table (will return as blank if nothing found)
OPEN REQUEST FOR
SELECT REQ_TYPE, '-' AS REQ_TYPE_STATUS FROM THIRDTABLE
WHERE THIRDTABLE.REQ_ID = REQUESTID;
END IF;
END IF;
这里也有类似的问题:我认为读这篇文章可以解决你的问题:-)