为什么我会收到“未找到数据”的信息将字符串变量传递到 PL/SQL where 子句时

发布于 2024-11-02 11:58:57 字数 2083 浏览 0 评论 0原文

我试图根据用户的网络 ID 获取用户的 ID 号,但由于某种原因,当我尝试在 SELECTWHERE 子句中传递其网络 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末的微笑 2024-11-09 11:58:57

如果您的 FOR 循环选择了不在远程表中的 user_name (例如:远程列有尾随空白),您将收到此错误,因为您没有处理 select 语句上的任何异常。我将通过添加异常处理程序并写入调试信息来调试它:

BEGIN
  FOR user_rec IN SELECT_PORTALUSERS LOOP
    -- Fetch Address Book # based on user ID
    BEGIN
    SELECT ABNum INTO v_addressbooknum
    FROM OWNER.TABLE@DBLINK
    WHERE UserID = user_rec.USER_NAME; -- TRIM column or variable if necessary
    EXCEPTION
      WHEN OTHERS THEN
         DBMS_OUTPUT.PUT_LINE('User: '||user_rec.user_name);
    END;
  END LOOP;
END;

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:

BEGIN
  FOR user_rec IN SELECT_PORTALUSERS LOOP
    -- Fetch Address Book # based on user ID
    BEGIN
    SELECT ABNum INTO v_addressbooknum
    FROM OWNER.TABLE@DBLINK
    WHERE UserID = user_rec.USER_NAME; -- TRIM column or variable if necessary
    EXCEPTION
      WHEN OTHERS THEN
         DBMS_OUTPUT.PUT_LINE('User: '||user_rec.user_name);
    END;
  END LOOP;
END;
岁月流歌 2024-11-09 11:58:57

似乎并非 PERSONS 中的所有用户也在 v_addressbooknum 中。

尝试使用以下 sql 查找 PERSONS 中存在但 v_addressbooknum 中不存在的所有用户,

SELECT USER_NAME FROM PERSONS
MINUS
SELECT userID FROM v_addressbooknum
;

然后用于

Exception
  When NO_DATA_FOUND Then
    DBMS_OUTPUT.PUT_LINE('no data found');
End;

处理此错误情况。

不要用“当其他人”来处理“没有找到数据”。

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

SELECT USER_NAME FROM PERSONS
MINUS
SELECT userID FROM v_addressbooknum
;

Then use

Exception
  When NO_DATA_FOUND Then
    DBMS_OUTPUT.PUT_LINE('no data found');
End;

to handle this error condition.

Do not use "when others" to handle "no data found".

仅一夜美梦 2024-11-09 11:58:57

看起来这是一个数据格式问题,我在 WHERE 子句中对列和变量进行了 TRIM 编辑,看起来它正确地返回了数据...奇怪,因为它们是两个 VARCHAR

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

    BEGIN
        -- Fetch Address Book # based on user ID
        SELECT ABNum INTO v_addressbooknum
        FROM OWNER.TABLE@DBLINK
        WHERE TRRIM(UserID) = TRIM(user_rec.USER_NAME);
    EXCEPTION
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('User: '||user_rec.USER_NAME);
    END;
  END LOOP;

END;

Looks like it was a dataformatting issue, I TRIMed both the column and the variable in the WHERE clause and it seems like it properly returns data... Odd since they are both VARCHARs

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

    BEGIN
        -- Fetch Address Book # based on user ID
        SELECT ABNum INTO v_addressbooknum
        FROM OWNER.TABLE@DBLINK
        WHERE TRRIM(UserID) = TRIM(user_rec.USER_NAME);
    EXCEPTION
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('User: '||user_rec.USER_NAME);
    END;
  END LOOP;

END;
じее 2024-11-09 11:58:57

您能输入表定义和几行吗?

我认为

SELECT ABNum INTO v_addressbooknum
        FROM OWNER.TABLE@DBLINK
        WHERE UserID = user_rec.USER_NAME;

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

SELECT ABNum INTO v_addressbooknum
        FROM OWNER.TABLE@DBLINK
        WHERE UserID = user_rec.USER_NAME;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文