传递无效的用户 ID,代码应显示消息,但 dbms_output 为空白

Posted

技术标签:

【中文标题】传递无效的用户 ID,代码应显示消息,但 dbms_output 为空白【英文标题】:Passing an invalid user id, code should display message but the dbms_output is blank 【发布时间】:2018-01-16 12:23:21 【问题描述】:

此过程应列出映射到活动用户的所有项目 ID,并且在传递无效用户 ID 时应打印“用户无效”。但是,没有显示任何消息。

PROCEDURE get_pid_info (p_return_status_o      OUT VARCHAR2,
                        p_error_message_o      OUT VARCHAR2,
                        p_user_id           IN     VARCHAR2)
IS
  CURSOR c_pid
  IS
       SELECT DISTINCT xpppa.project_id,
                       papf.person_type_id,
                       xpppp.end_date_active,
                       papf.person_id,
                       COUNT (DISTINCT xpppa.project_id) pid_count
         FROM xxcas_prj_pa_projects_all xpppa,
              xxcas_prj_pa_project_players xpppp,
              per_all_people_f papf
        WHERE     xpppa.project_id = xpppp.project_id
              AND xpppp.person_id = papf.person_id
              AND papf.person_id = 61--p_user_id
              AND xpppp.project_role_type = 'PROJECT MANAGER'
              AND papf.person_type_id = 6
              and sysdate between xpppa.start_date and nvl(xpppa.completion_date,sysdate+1)
              and sysdate between xpppp.start_date_active and nvl(xpppp.end_date_active,sysdate+1)
              AND EXISTS
                     (SELECT 1
                        FROM pa_lookups
                       WHERE     lookup_type = 'XXCAS_PRJ_USER_DETAILS'
                             AND description=papf.email_address
                             AND enabled_flag = 'Y'
                             AND SYSDATE BETWEEN start_date_active
                                             AND NVL (end_date_active,
                                                      SYSDATE + 1))
     GROUP BY xpppa.project_id,
              papf.person_type_id,
              xpppp.end_date_active,
              papf.person_id;
BEGIN
  FOR l_rec IN c_pid
  LOOP
     IF l_rec.project_id IS NOT NULL
     THEN
        dbms_output.put_line (
              'The Projects mapped to the active user ID are : '
           || l_rec.project_id);
     ELSIF l_rec.end_date_active < SYSDATE
     THEN
        dbms_output.put_line (
           'The user has been end dated in the system on : ' || l_rec.end_date_active);
     ELSIF l_rec.pid_count = 0
     THEN
        dbms_output.put_line (
           'There are no projects mapped to the user having PM role');
     ELSE 
        dbms_output.put_line ('Please check the user ID passed');
     END IF;

     IF SQL%NOTFOUND 
     THEN
       dbms_output.put_line('Entered user id is not valid');
     end if;

  END LOOP;
EXCEPTION
  WHEN NO_DATA_FOUND
  THEN
    dbms_output.put_line ('Please provide valid user ID');
  WHEN OTHERS
  THEN
    dbms_output.put_line ('Error in get_pid' || SQLERRM);
END get_pid_info;

匿名块:

declare
    status varchar2(30);
    msg varchar2(30);
begin
    --DBMS_OUTPUT.ENABLE('20000000'); 
    XXCAS_PRJ_PROJECT_DTLS.GET_PID_INFO(status,msg,61);
end; 

【问题讨论】:

需要检查游标循环是否找到要处理的行。 sql%notfound 不会那样做。 【参考方案1】:

首先,如果没有要处理的行,则不会进入循环,因此不会执行其中的任何检查。

其次,SQL% cursor attributes 适用于隐式游标,而不是像您的过程中的 c_pid 这样的命名游标。最简单的方法是声明一个布尔变量,将其初始化为FALSE,并在循环中将其设置为TRUE;然后你可以在循环后检查它的值。 (或者如果您想要实际的行数,请使用 c_pid%rowcount 并将其初始化为 0。)

我假设这是您实际过程的简化版本,因为 dbms_output 在生产代码中通常没有用处,除非调用者设置为捕获它。

【讨论】:

以上是关于传递无效的用户 ID,代码应显示消息,但 dbms_output 为空白的主要内容,如果未能解决你的问题,请参考以下文章

Angular 4 错误消息应链接到无效的 formControl

C ++循环:如何让程序在用户输入无效输入时多次重复“错误”消息

Firebase 云消息传递 php 脚本返回无效注册

如何在登录失败时显示错误消息?

GCM 响应是无效的 Android 应用注册 ID

数据未保存在会话变量中