为什么我会收到“未找到数据”的信息将字符串变量传递到 PL/SQL where 子句时
我试图根据用户的网络 ID 获取用户的 ID 号,但由于某种原因,当我尝试在 SELECT
的 WHERE
子句中传递其网络 ID 时在此 PL/SQL 语句中我收到错误。
但是,如果我将 user_rec.USER_NAME
中的内容输出到控制台,则有一个有效值,并且如果我对字符串进行硬编码,例如: WHERE UserID = 'USERNAME'
它也可以工作正如预期的那样。似乎只有 WHERE
子句中的字符串变量失败......?
ORA-01403: no data found
ORA-06512 at line 12
01403.00000 - "no data found"
*Cause
*Action
Error at line 1
PL/SQL:
DECLARE
v_addressbooknum number;
v_addresstype nchar(3);
CURSOR SELECT_PORTALUSERS is
SELECT USER_NAME FROM PERSONS WHERE DEFAULT_GROUP <> 'Employees';
BEGIN
FOR user_rec IN SELECT_PORTALUSERS LOOP
-- Fetch Address Book # based on user ID
SELECT ABNum INTO v_addressbooknum
FROM OWNER.TABLE@DBLINK
WHERE UserID = user_rec.USER_NAME;
END LOOP;
END;
========================================= ================================ 编辑
这是我根据 DCookie 的建议运行的新 PL/SQL
DECLARE
v_addressbooknum number;
v_addresstype nchar(3);
CURSOR SELECT_PORTALUSERS is
SELECT USER_NAME FROM PERSONS WHERE DEFAULT_GROUP <> 'Employees';
BEGIN
FOR user_rec IN SELECT_PORTALUSERS LOOP
-- DEBUG --
DBMS_OUTPUT.PUT_LINE('DEBUG Found User: ' || user_rec.USER_NAME);
-- END DEBUG --
BEGIN
-- Fetch Address Book # based on user ID
SELECT ABNum INTO v_addressbooknum
FROM OWNER.TABLE@DBLINK
WHERE UserID = user_rec.USER_NAME;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('User: '||user_rec.USER_NAME);
END;
END LOOP;
-- DEBUG --
DBMS_OUTPUT.PUT_LINE('DONE');
-- END DEBUG --
END;
这是我得到的输出,您可以查看是否在 user_rec.user_name
中找到值。如果我将找到的用户 ID 之一硬编码到 where 子句中,则它可以正常工作(意味着用户 ID 存在于正在执行提取的表中)。
DEBUG Found User: USER846
User: USER846
DEBUG Found User: USER241
User: USER241
DEBUG Found User: USER780
User: USER780
DEBUG Found User: USER783
User: USER783
DEBUG Found User: USER294
User: USER294
DONE
I'm trying to get a user's ID number based on their network ID, but for some reason when I try to pass in their network ID in the WHERE
clause of the SELECT
statement in this PL/SQL I get an error.
However if I output what is in user_rec.USER_NAME
to the console there is a valid value, and if I hardcode a string ex: WHERE UserID = 'USERNAME'
it also works as expected. It seems to only fail for string variables in the WHERE
clause.....?
ORA-01403: no data found
ORA-06512 at line 12
01403.00000 - "no data found"
*Cause
*Action
Error at line 1
PL/SQL:
DECLARE
v_addressbooknum number;
v_addresstype nchar(3);
CURSOR SELECT_PORTALUSERS is
SELECT USER_NAME FROM PERSONS WHERE DEFAULT_GROUP <> 'Employees';
BEGIN
FOR user_rec IN SELECT_PORTALUSERS LOOP
-- Fetch Address Book # based on user ID
SELECT ABNum INTO v_addressbooknum
FROM OWNER.TABLE@DBLINK
WHERE UserID = user_rec.USER_NAME;
END LOOP;
END;
=====================================================================
EDIT
Here is the new PL/SQL I ran based on DCookie's suggestion
DECLARE
v_addressbooknum number;
v_addresstype nchar(3);
CURSOR SELECT_PORTALUSERS is
SELECT USER_NAME FROM PERSONS WHERE DEFAULT_GROUP <> 'Employees';
BEGIN
FOR user_rec IN SELECT_PORTALUSERS LOOP
-- DEBUG --
DBMS_OUTPUT.PUT_LINE('DEBUG Found User: ' || user_rec.USER_NAME);
-- END DEBUG --
BEGIN
-- Fetch Address Book # based on user ID
SELECT ABNum INTO v_addressbooknum
FROM OWNER.TABLE@DBLINK
WHERE UserID = user_rec.USER_NAME;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('User: '||user_rec.USER_NAME);
END;
END LOOP;
-- DEBUG --
DBMS_OUTPUT.PUT_LINE('DONE');
-- END DEBUG --
END;
And here is the output I get, you can see if finds values in the user_rec.user_name
. And if I hardcode one of the user IDs found into the where clause it works properly (meaning the User IDs exist in the tabled the fetch is being executed against).
DEBUG Found User: USER846
User: USER846
DEBUG Found User: USER241
User: USER241
DEBUG Found User: USER780
User: USER780
DEBUG Found User: USER783
User: USER783
DEBUG Found User: USER294
User: USER294
DONE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 FOR 循环选择了不在远程表中的 user_name (例如:远程列有尾随空白),您将收到此错误,因为您没有处理 select 语句上的任何异常。我将通过添加异常处理程序并写入调试信息来调试它:
If your FOR loop ever selects a user_name that is not in the remote table (ex: remote column has trailing blanks) you'll get this error, because you're not handling any exceptions on the select statement. I'd debug this by adding an exception handler and writing debug info:
似乎并非 PERSONS 中的所有用户也在 v_addressbooknum 中。
尝试使用以下 sql 查找 PERSONS 中存在但 v_addressbooknum 中不存在的所有用户,
然后用于
处理此错误情况。
不要用“当其他人”来处理“没有找到数据”。
It just seems that not all users that are in PERSONS are also in v_addressbooknum.
Try the following sql to find all users that exist in PERSONS but not in v_addressbooknum
Then use
to handle this error condition.
Do not use "when others" to handle "no data found".
看起来这是一个数据格式问题,我在
WHERE
子句中对列和变量进行了TRIM
编辑,看起来它正确地返回了数据...奇怪,因为它们是两个 VARCHARLooks like it was a dataformatting issue, I
TRIM
ed both the column and the variable in theWHERE
clause and it seems like it properly returns data... Odd since they are bothVARCHAR
s您能输入表定义和几行吗?
我认为
table@dblink 中的 UserId 列与 user_rec.USER_NAME 不是同一列;
在您发布的代码中,选择会出现异常,每行上都有 no_data_found
could you please put the table definition and a couple of rows?
i think that
the column UserId from table@dblink is not the same column as user_rec.USER_NAME;
in the code that you have posted, the select goes with the exception with no_data_found on each row