2016-04-15 157 views
1

我寫了一個簡單的plsql函數來計算字符串中的字數。計算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 
" 

請提出問題可能是什麼。

============================================== ==================================更新了回報,並將默認計數變量更改爲其他變量,並且它工作正常。

但現在我在這裏面臨的一個合乎邏輯的問題:

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

當我運行這個它給了我作爲6個輸出,雖然7個字都在那裏,我的字計算是基於空間計數,所以它失敗了。

有人可以建議我一個更好的邏輯來克服這種情況?

謝謝!

謝謝, Dex。

+2

你可以嘗試'在聲明中返回number' –

+0

SQL是不是一個很好的平臺做到字數是否有一個應用層或東西,你可以做到這一點的。? –

回答

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 
0

只需使用regexp_count功能與模式\ w +這意味着,任何單詞字符以最小的一個occurence的。和未定義最大:

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