计算plsql

时间:2016-04-15 16:42:38

标签: sql plsql oracle12c

我编写了一个简单的plsql函数来计算字符串中的单词数。

create or replace function GetWordsCount(instr in varchar2) return varchar2 
is
count number;
  outstr varchar2(2000);
  l1 number;
  l2 number;
begin
  outstr := rtrim(ltrim(REGEXP_REPLACE (instr, '\s{2,}', ' ')));
  l1 := length(outstr);
  l2 := lenth(replace(outstr,' ',''));
  count := l1-l2+1;
  return count;
end;

虽然当我使用查询在sql中测试此函数时它可以工作,但是当我尝试构建为函数时,它会给我以下错误。

SQLDEV:LINK:VS_DEV_NAV:FUNCTION:GETWORDSCOUNT:5:10:5,10:oracle.dbtools.raptor.controls.grid.DefaultDrillLink    "PLS-00103: Encountered the symbol "VARCHAR2" when expecting one of the following:

   := . ( @ % ;
The symbol ":=" was substituted for "VARCHAR2" to continue.
"
SQLDEV:LINK:VS_DEV_NAV:FUNCTION:GETWORDSCOUNT:5:24:5,24:oracle.dbtools.raptor.controls.grid.DefaultDrillLink    "PLS-00103: Encountered the symbol "=" when expecting one of the following:

   . ( * % & = - + ; < / > at in is mod remainder not rem
   <an exponent (**)> <> or != or ~= >= <= <> and or like like2
   like4 likec between || multiset member submultiset
"
SQLDEV:LINK:VS_DEV_NAV:FUNCTION:GETWORDSCOUNT:8:10:8,10:oracle.dbtools.raptor.controls.grid.DefaultDrillLink    "PLS-00103: Encountered the symbol "=" when expecting one of the following:

   constant exception <an identifier>
   <a double-quoted delimited-identifier> table long double ref
   char time timestamp interval date binary national character
   nchar
The symbol "<an identifier>" was substituted for "=" to continue.
"
SQLDEV:LINK:VS_DEV_NAV:FUNCTION:GETWORDSCOUNT:9:6:9,6:oracle.dbtools.raptor.controls.grid.DefaultDrillLink  "PLS-00103: Encountered the symbol "=" when expecting one of the following:

   constant exception <an identifier>
   <a double-quoted delimited-identifier> table long double ref
   char time timestamp interval date binary national character
   nchar
"

请说明问题所在。

=============================================== =================================更新了返回并将默认计数变量更改为其他变量并且它有效。< / p>

但现在我在这里遇到了一个逻辑问题:

declare 
c number;
begin
  c:=getwordscount('Hello World.Welcome to the Pl/Sql world!');
  dbms_output.put_line(c);
end;

当我运行它时,它输出为6,虽然有7个单词,但我的单词计算基于空间计数,所以它失败了。

有人可以建议我更好的逻辑来克服这种情况吗?

谢谢!

谢谢, 地塞米松。

2 个答案:

答案 0 :(得分:1)

这样的东西......决定其他字符不应该是单词的一部分,并将它们添加到匹配模式中。确保逃避可能具有特殊含义的字符,例如 - 和(。

with a (test_text) as 
   (
    select 'Hello World.Peace  on Earth!  Hurray!.. .# This is good'
    from dual
   )
select regexp_count(test_text, '[^ .?!#\()]+') as word_count from a
/
WORD_COUNT
----------
         9

答案 1 :(得分:0)

只需使用带有 \ w + 模式的regexp_count函数,这意味着,任意字符至少出现一次且未定义的最大值:

SELECT regexp_count('I have written a simple plsql function to calculate the number of words in a string.'
                   ,'\w+') 
  FROM dual