Plsql 如何在PL/SQL游标中获取unqiue结果集?

Plsql 如何在PL/SQL游标中获取unqiue结果集?,plsql,cursor,unique,distinct,Plsql,Cursor,Unique,Distinct,我想使用此过程显示用户名和移动电话号码,使用select时结果集如下: 申报 当程序运行时,我得到以下结果: 错误ORA-01722:无效号码 ORA-06512:“ABPROD.SHAREPOOL”,第24行 当我在光标中使用unique或distinct时,不会显示任何内容。 代码源: create or replace procedure sharepool (assignment in varchar2,myorgname in varchar2) is rightid T_C

我想使用此过程显示用户名和移动电话号码,使用select时结果集如下:

申报

当程序运行时,我得到以下结果:

错误ORA-01722:无效号码
ORA-06512:“ABPROD.SHAREPOOL”,第24行

当我在光标中使用unique或distinct时,不会显示任何内容。 代码源:

create or replace procedure sharepool (assignment  in varchar2,myorgname in varchar2)  is
rightid   T_CLM_AP30_RIGHT.RIGHT_ID%type;
orgid t_clm_ap30_org.org_id%type;

begin

  select t.right_id  into rightid   from T_CLM_AP30_RIGHT t where t.rightdesc=trim(assignment);
dbms_output.put_line(rightid||trim(myorgname)||assignment);
  select t.org_id into orgid  from t_clm_ap30_org t  where t.orgname=trim(myorgname);
  dbms_output.put_line(orgid);

      declare 
      cursor namelist  is select distinct a.username,a.mobile from t_clm_ap30_user a, T_CLM_AP30_RIGHT_AUTH t where a.user_id=t.user_id and t.right_id=rightid and  t.poolorgrange=orgid ;
      begin 
        for c in namelist
          loop

            dbms_output.put_line(c.username||'   '||c.mobile);
            end loop;
        end;



end sharepool;

无效\u编号
错误表示字符串转换为数字失败。这意味着您的连接条件之一是比较字符串列和数字列,并且字符串列中的值不能转换为数字

ORA-06512:“ABPROD.SHAREPOOL”第24行

第24行与您发布的代码不一致,可能是在实际源代码的翻译过程中丢失的。此外,您还没有发布表说明,因此我们无法确定要查看哪些列

所以这里有一个猜测

其中一个(或多个)联接具有隐式数字转换:

where a.user_id = t.user_id 
and t.right_id = rightid 
and  t.poolorgrange = orgid 
也就是说,
t\u clm\u ap30\u user.user\u id
是数字,而
t\u clm\u ap30\u RIGHT\u AUTH.user\u id
不是数字,反之亦然。或者
T\u CLM\u AP30\u RIGHT\u AUTH.RIGHT\u id
是数字,而
T\u CLM\u AP30\u RIGHT.RIGHT\u id
不是数字,反之亦然。或者
T\u CLM\u AP30\u RIGHT\u AUTH.poolorgrange
是数字,而
T\u CLM\u AP30\u org.org\u id
不是数字,反之亦然

只有您才能理解这一点,因为只有您才能看到您的模式。一旦确定了将字符串列与数字列进行比较的联接,就需要查询该列以查找无法转换为数字的数据

假设
T\u CLM\u AP30\u RIGHT\u AUTH.poolorgrange
是rogue字符串。您可以通过此查询查看哪些行比较麻烦:

select * from T_CLM_AP30_RIGHT_AUTH
where translate (poolorgrange, 'x1234567890', 'x') is not null;
translate()
函数去掉数字。所以剩下的任何东西都不能转换成数字

T_CLM_AP30_RIGHT_AUTH.poolorgrange是varchar2,T_CLM_AP30_org.org_id是数字

通过将
t\u clm\u ap30\u org.org\u id
显式转换为字符串,可以避免此错误:

select distinct a.username, a.mobile 
from t_clm_ap30_user a, 
    T_CLM_AP30_RIGHT_AUTH t 
where a.user_id = t.user_id 
and t.right_id = rightid 
and  t.poolorgrange = to_char(orgid) ;

显然,您不会获得这些字母数字值的匹配项,但查询将运行。

您在此处发布的代码显然不是您正在执行的代码,因此不清楚您希望我们如何提供帮助。我们所能说的是,无效的数字错误表示将字符串转换为数字失败。这可能意味着您的一个连接条件正在比较字符串列和数字列,并且字符串列中的值无法转换为数字。我已发布了完整代码。当我运行它时,我得到以下信息:ORA-01722:无效数字ORA-06512:位于“ABPROD.SHAREPOOL”,第24行ORA-06512:在第1行谢谢你。你是对的。T_CLM_AP30_right_AUTH.poolorgrange是varchar2,T_CLM_AP30_org.org_id是数字。