Oracle Procedure return 算作一个单词

Oracle Procedure to return count as a word

我正在尝试创建一个过程,它接受一个 table 值 (varchar2),计算它出现的次数,然后 returns 一个语句告诉用户它发生了多少次在 table 中是一个单词而不是一个数字(三而不是 3)。 到目前为止我的代码是:

create or replace procedure user_search (x in varchar2) is y int;

begin

select count(*) as z into y from userpermissions where x=username; dbms_output.put_line(to_char(y) || ' document(s) found under user: ' || x);

exception

when no_data_found then dbms_output.put_line('no documents for user: ' || x);

end;

当我 运行 这个时,它会说 'pl/sql procedure completed successfully' ,但不会说 return 任何东西。试图找出我在这里做错了什么,任何帮助将不胜感激

你做错了几件事。

直接问题(“不是return任何东西”)与您的程序无关;您需要先 运行 set serveroutput on,以便在程序完成后打开 dbms_output 缓冲区在屏幕上的显示。你做到了吗?

你的异常永远不会到达;如果在您的 table 中找不到 x 作为用户名,查询仍然是 return 行:在这种情况下计数将为零。如果您必须将此视为例外(and/or 使用单词“none”而不是单词“零”),您将需要以不同的方式处理它。

一旦你开始看到你的输出,你会注意到 to_char(<number>) 并没有神奇地将数字转换成英语表示的单词。您认为是这样吗?或者您知道如何改变它吗?在这个阶段,您只是在测试目前为止得到的结果?

如果您在最后一部分需要帮助,这里有一个提示:

select deptno, to_char(to_date(count(*), 'j'), 'jsp') as english_num
from   scott.emp
group  by deptno
;

    DEPTNO ENGLISH_NUM    
---------- ---------------
        30 six            
        20 five           
        10 three       

如果您不明白它是如何工作的,请查看 'j' 和 'jsp' 格式模型 to_date()to_char()