Invalid identifier error for v_MONTH in dynamic query - oracle

I'm running following procedure inside a package to post entries in table ledger_stat_dly.
I've written dynamic query to replace case statements but I am facing following error.
Could you please suggest why V_Month is invalid identifier error popping up while it is defined properly in procedure.
Thanks in advance for help.
Error is:
ORA-00904: "V_MONTH": invalid identifier
(
V_IDENTITY_CODE NUMBER,
V_CONSOLIDATION_CD NUMBER,
V_FINANCIAL_ELEM_ID NUMBER,
V_ORG_UNIT_ID NUMBER,
V_GL_ACCOUNT_ID NUMBER,
V_COMMON_COA_ID NUMBER,
V_PRODUCT_1_ID NUMBER,
V_PRODUCT_ID NUMBER,
V_PRODUCT_3_ID NUMBER,
V_DATE DATE,
V_AMOUNT NUMBER,
V_MEMO_GL_ACCOUNT_ID NUMBER DEFAULT 0,
V_POSTINGTYPE CHAR DEFAULT 'N',
V_BALANCE_TYPE_CD NUMBER DEFAULT 0
)
IS
V_CNT NUMBER;
V_MONTH CHAR(2);
V_MO NUMBER;
V_YEAR_S NUMBER;
-- variables store result of dynamic cursor
V_SL VARCHAR2(2500);
V_TARGET_COLUMN VARCHAR2(6 CHAR);
BEGIN
IF V_POSTINGTYPE = 'N' THEN
IF NVL(V_AMOUNT,0) <> 0 THEN
V_MO := (MONTH(V_DATE));
V_MONTH := LPAD(V_MO,2,'0');
V_YEAR_S := (YEAR(V_DATE));
V_TARGET_COLUMN := CONCAT('DAY_',LPAD(TO_CHAR(DAY(V_DATE)),2,'0'));
EXECUTE IMMEDIATE UTL_LMS.FORMAT_MESSAGE('UPDATE /*+ index(a LEDGER_STAT_DLY_IDX02_IN) */ LEDGER_STAT_DLY A
SET %s = NVL(%s,0) + NVL(V_AMOUNT,0)
WHERE IDENTITY_CODE = NVL(V_IDENTITY_CODE,0)
AND YEAR_S = NVL(V_YEAR_S,0)
AND MONTH_NO = NVL(V_MONTH,0)',V_TARGET_COLUMN, V_TARGET_COLUMN);
END IF;
END IF; --CLOSURE FOR POSTING TYPE IF STATEMENT
END IN_LEDGER_STAT_DAILY;

Because you composed the SQL statement as a string the PLSQL engine does NOT substituted for the variable name (their just part of a literal string), therefore the SQL engine sees the string 'V_MONTH' but there is no column by that name thus invalid identifier. If you stay with dynamic SQL you'll have to do value substitution yourself. The same also applies to the other variables. So:
EXECUTE IMMEDIATE UTL_LMS.FORMAT_MESSAGE(
'UPDATE /*+ index(a LEDGER_STAT_DLY_IDX02_IN) */ LEDGER_STAT_DLY A
SET %s = NVL(%s,0) + NVL(%s ,0)
WHERE IDENTITY_CODE = NVL(%s ,0)
AND YEAR_S = NVL(%s ,0)
AND MONTH_NO = NVL(%s ,0)'
,V_TARGET_COLUMN, V_TARGET_COLUMN ,V_AMOUNT,V_IDENTITY_CODE,V_YEAR_S,V_MONTH);
You may also need to do any necessary format conversions.

Related

Show the output of a procedure in Oracle

The procedure uses the previous function to display the list of the products: num, designation and mention on the application.
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION STORE(num_produit IN INTEGER) RETURN VARCHAR AS
N INTEGER := 0;
incre INTEGER := 0;
BEGIN
SELECT SUM(qte) INTO N FROM Ligne_Fact WHERE num_produit = produit;
IF N > 15 THEN
RETURN 'fort';
ELSIF N > 11 THEN
RETURN 'moyen';
END IF;
RETURN 'faible';
END;
/
CREATE OR REPLACE PROCEDURE SHOW_PRODUITS IS
SOME_VAR VARCHAR2(256);
BEGIN
SELECT num, designation, STORE(num) INTO SOME_VAR FROM Produit;
dbms_output.enable();
dbms_output.put_line('result : '|| SOME_VAR);
END;
/
BEGIN
SHOW_PRODUITS;
END;
/
I am sure that all the tables are filled with some dummy data, but I am getting the following error:
Function STOCKER compiled
Procedure AFFICHER_PRODUITS compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
4/4 PL/SQL: SQL Statement ignored
4/56 PL/SQL: ORA-00947: not enough values
Errors: check compiler log
Error starting at line : 28 in command -
BEGIN
AFFICHER_PRODUITS;
END;
Error report -
ORA-06550: line 2, column 5:
PLS-00905: object SYSTEM.SHOW_PRODUITS is invalid
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
The first major mistake you've made is to create your objects in SYSTEM schema. It, just like SYS, are special and should be used only for system maintenance. Create your own user and do whatever you're doing there.
As of your question: select returns 3 values, but you're trying to put them into a single some_var variable. That won't work. Either add another local variables (for num and designation), or remove these columns from the select:
CREATE OR REPLACE PROCEDURE SHOW_PRODUITS IS
SOME_VAR VARCHAR2(256);
BEGIN
SELECT STORE(num) INTO SOME_VAR FROM Produit; --> here
dbms_output.enable();
dbms_output.put_line('result : '|| SOME_VAR);
END;
/
Code, as you put it, presumes that produit contains a single record (it can't be empty nor it can have 2 or more rows because you'll get various errors).
Maybe you wanted to access all rows; in that case, consider using a loop, e.g.
CREATE OR REPLACE PROCEDURE SHOW_PRODUITS IS
SOME_VAR VARCHAR2(256);
BEGIN
FOR cur_r IN (SELECT num, designation, STORE(num) some_var FROM Produit) LOOP
dbms_output.put_line(cur_r.num ||', '|| cur_r.designation ||', result : '|| cur_r.some_var);
END LOOP;
END;
/
Then
set serveroutput on
BEGIN
SHOW_PRODUITS;
END;
/
You are trying to compile some stored routine which calls another stored routine named AFFICHER_PRODUITS and that routine calls SHOW_PRODUITS but SHOW_PRODUITS does not compile, hence the error. (By the way, it is recommended not to create your own stored routines in the SYSTEM schema.)
SHOW_PRODUITS does not compile because of this line:
SELECT num, designation, STORE(num) INTO SOME_VAR FROM Produit;
It appears that you want to get the query results as a string. In order to do that, you need to concatenate the column values, i.e.
SELECT num || designation || STORE(num) INTO SOME_VAR FROM Produit;
Of-course if all you want to do is display the query results, using DBMS_OUTPUT, you can declare a separate variable for each column.
CREATE OR REPLACE PROCEDURE SHOW_PRODUITS IS
SOME_NUM Produit.num%type;
SOME_DES Produit.designation%type;
SOME_VAR VARCHAR2(6);
BEGIN
SELECT num
,designation
,STORE(num)
INTO SOME_NUM
,SOME_DES
,SOME_VAR
FROM Produit;
dbms_output.enable();
dbms_output.put_line('result : ' || SOME_NUM || SOME_DES || SOME_VAR);
END;
Note that if the query returns more than one row, you will [probably] need to use a cursor.
CREATE OR REPLACE PROCEDURE SHOW_PRODUITS IS
SOME_NUM Produit.num%type;
SOME_DES Produit.designation%type;
SOME_VAR VARCHAR2(6);
--
CURSOR c1 IS
SELECT num, designation, STORE(num) FROM Produit;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO SOME_NUM, SOME_DES, SOME_VAR;
EXIT WHEN c1%NOTFOUND;
dbms_output.put_line('result : ' || SOME_NUM || SOME_DES || SOME_VAR);
END LOOP;
CLOSE c1;
END;

I am experiencing an error with one of my oracle stored procedures with error ORA:24344

I am experiencing an error with one of my oracle stored procedures with error ORA:24344 when trying to create a stored procedure
I am getting the following error message
ORA-24344: success with compilation error
PL/SQL: ORA-00927: missing equal sign
PL/SQL: SQL Statement ignored
CREATE OR REPLACE PROCEDURE sp_comms_update_stg (
ssms_key IN VARCHAR2,
spolicyNumber IN VARCHAR2,
sclientKey IN VARCHAR2,
sclientReference IN VARCHAR2,
sresult OUT SYS_REFCURSOR
)
IS
BEGIN
UPDATE stg_update_email
SET
sms_key := ssms_key,
policy_number := spolicyNumber,
client_key := sclientKey,
process_status := 'Processed'
WHERE client_reference = sclientReference;
INSERT INTO EVENTLOG VALUES(seq_eventlog.NEXTVAL, spolicyNumber , (select
to_date(sysdate) from dual),
to_char(sysdate,'HH24:MI:SS'), 101, null, 1, 'Updated stg_update_email',
'stg_update_email successfully updated', 'P', var_Client, null, null);
sresult:= true;
COMMIT;
EXCEPTION
WHEN OTHERS Then
Rollback;
sresult := false;
RAISE_APPLICATION_ERROR (-20000,'ERROR IN EXECUTING PROCEDURE
SP_UNDEL_UPD_STG - '|| chr(13)||chr(10) || UPPER(SQLERRM) ||
chr(13)||chr(10));
END;
ORA-24344: success with compilation error
PL/SQL: ORA-00927: missing equal sign
PL/SQL: SQL Statement ignored
That was pointing out that you had used the PL/SQL assignment operator := instead of the SQL one = in your update statement. But you say despite having fixed that you still cannot compile your procedure. Maybe it's just that the compiler doesn't like assigning a Boolean to a sys_refrcursor parameter, may there's more to it.
I suggest you run this query to see what other errors the compiler has spotted.
select * from user_errors
where name = 'SP_COMMS_UPDATE_STG'
IDEs like Allround Automations PL/SQL Developer and Oracle SQL Developer will do this for us automatically (in Orcale SQL Developer you need to click on the Errors tab) but it doesn't do any harm to know the explicit query.
See the comments; fixing these issues should make your procedure compile:
CREATE OR REPLACE PROCEDURE sp_comms_update_stg(
ssms_key IN VARCHAR2,
spolicyNumber IN VARCHAR2,
sclientKey IN VARCHAR2,
sclientReference IN VARCHAR2,
sresult OUT boolean /* based on your code, you probably need a BOOLEAN */
) IS
BEGIN
UPDATE stg_update_email
SET sms_key = ssms_key, /* = and not := */
policy_number = spolicyNumber, /* = and not := */
client_key = sclientKey, /* = and not := */
process_status = 'Processed' /* = and not := */
WHERE client_reference = sclientReference;
INSERT INTO EVENTLOG
VALUES (
seq_eventlog.NEXTVAL,
spolicyNumber,
sysdate, --(SELECT TO_DATE(SYSDATE) FROM DUAL), /* let's simplify! also, to_date(sysdate) makes no sense: sysdate already is a date */
/* or even trunc(sysdate) if you don't want time informations*/
TO_CHAR(SYSDATE, 'HH24:MI:SS'),
101,
NULL,
1,
'Updated stg_update_email',
'stg_update_email successfully updated',
'P',
var_Client, /* where is var_Client defined ? */
NULL,
NULL
);
sresult := TRUE;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
sresult := FALSE;
RAISE_APPLICATION_ERROR(-20000, 'ERROR IN EXECUTING PROCEDURE SP_UNDEL_UPD_STG - '
|| CHR(13) || CHR(10) || UPPER(SQLERRM) || CHR(13) || CHR(10));
END;

ORACLE APEX PL SQL Procedure Error

I keep getting a the error: success with compilation error. What am I doing wrong with my code? I tried it in sqlfiddle but I get an invalid SQL statement error. As far as I know, this is the correct syntax for PL/SQL
create or replace PROCEDURE PRC_CALC
(W_ORDERID_IN IN NUMBER)
AS
W_PARTSERVICEID VARCHAR2(10);
W_EXIST_FLAG NUMBER(1) :=0;
W_SUBTOTAL NUMBER(9) :=0;
W_TAX NUMBER(9) :=0.07;
W_DISCOUNT NUMBER(9) :=0;
W_TOTAL NUMBER(9) :=0;
BEGIN
SELECT COUNT(*)
INTO W_EXIST_FLAG
FROM tblJobOrders
WHERE fldOrderId = W_ORDERID_IN;
IF W_EXIST_FLAG = 1 THEN
CURSOR CUR_ORDERCHARGES IS
SELECT fldPartServiceId
FROM tblOrderCharges
WHERE fldOrderId = W_ORDERID_IN;
OPEN CUR_ORDERCHARGES;
LOOP
FETCH CUR_ORDERCHARGES
INTO W_PARTSERVICEID
EXIT WHEN CUR_ORDERCHARGES%NOTFOUND;
SELECT fldPartServiceAmount, fldDiscountPercent
INTO W_SUBTOTAL, W_DISCOUNT
FROM tblPartsServices
WHERE fldPartServiceId = W_PARTSERVICEID;
W_DISCOUNT := (W_SUBTOTAL*(W_DISCOUNT*.01));
W_TAX := (W_TOTAL*W_TAX);
W_TOTAL := W_SUBTOTAL - W_DISCOUNT;
W_TOTAL := W_TOTAL + W_TAX;
htp.prn('Your subtotal is: $' ||W_SUBTOTAL||'<br>');
htp.prn('Your Discount is: $' ||W_DISCOUNT||'<br>');
htp.prn('Your Tax is: $' ||W_TAX||'<br>');
htp.prn('Your Total is: $' ||W_TOTAL||'<br>');
END LOOP;
CLOSE CUR_ORDERCHARGES;
ELSE
htp.prn('The Order Id: '||W_ORDERID_IN||' does not exist in the database');
END IF;
END;
Your variable declarations need work
W_PARTSERVICEID VARCHAR2; should have a size such as W_PARTSERVICEID VARCHAR2(250);
Your number declarations will work but are better to specify a size as well
W_EXIST_FLAG NUMBER; should be W_EXIST_FLAG NUMBER(9);
W_EXIST_ORDER_FLAG is not declared and should be as well.
As a programming practice that goes beyond your question you should check that the values coming into the procedure and in the cursor are not null or zero.
The CURSOR CUR_ORDERCHARGES should be declared with the other declarations or put inside a new DECLARE BEGIN END block
and you are missing a semi colon when you FETCH the cursor, it should be
FETCH CUR_ORDERCHARGES
INTO W_PARTSERVICEID;
EXIT WHEN CUR_ORDERCHARGES%NOTFOUND;
The W_EXIST_ORDER_FLAG used in the first query is undefined. Perhaps, you meant W_EXIST_FLAG?

Updating table in SQLPLUS (Stored Procedure Loop with Comma Delimited Column)

Having some trouble writing my stored procedure. Using Oracle 11g
Goal: I want to be able to create separate rows in my table "info_table" from my table "places_table" with the column alternatenames. Under the column alternatenames from places_table, there is a comma delimited string with multiple alternate names. I want to create a row for each one of these alternate names in table "info_table".
ex of alternatenames column string:
Beijing,Beijingzi,Pei-ching-tzu
what I am hoping to achieve
ID Name
100000000 Beijing
100000001 Beijingzi
100000002 Pei-ching-tzu
Currently my code looks like this:
CREATE TABLE INFO_TABLE
(
INFOID NUMBER PRIMARY KEY,
NAME VARCHAR2(500),
LANGUAGE VARCHAR2(40),
STATUS VARCHAR2(50),
COUNTRY_CODE CHAR (10),
COUNTRY_CODE_2 CHAR (10),
GID CHAR(10),
SUPPLIERID CHAR(10),
LAST_MODIFIED CHAR(50)
);
CREATE SEQUENCE INFO_COUNTER
START WITH 100000000;
CREATE PROCEDURE LOAD_ALTERNATE_NAMES(ALTERNATENAMES_COLUMN VARCHAR2)
AS
COMMA_FINDER NUMBER := 1;
BEGIN
IF ALTERNATENAMES_COLUMN IS NOT NULL
THEN
<<SEPARATE_ALTERNATENAMES>> WHILE COMMA_FINDER!=0 LOOP
INSERT INTO INFO_TABLE
(INFOID, NAME, LANGUAGE, STATUS, COUNTRY_CODE, COUNTRY_CODE_2, GID, SUPPLIERID, LAST_MODIFIED)
VALUES
(INFO_COUNTER, SUBSTR(ALTERNATENAMES_COLUMN, INSTR(P.ALTERNATENAMES, ',', COMMA_FINDER+1)), NULL, 'ALTERNATE', P.COUNTRY_CODE, P.COUNTRY_CODE_2, P.GID, NULL, P.LASTMODIFIED)
FROM INFO_TABLE I, PLACES_TABLE P;
COMMA_FINDER := INSTR(ALTERNATENAMES, ',', COMMA_FINDER);
END LOOP SEPARATE_ALTERNATENAMES;
COMMA_FINDER:=1;
ENDIF;
END
/
LOAD_ALTERNATE_NAMES(SELECT ALTERNATENAMES FROM PLACES_TABLE);
currently the problem is that my INSERT statement in my loop is giving me "SQL Statement Ignored" and I am not sure why. I have taken a look at the stored procedure and loop documentation but can't figure out if I am doing something wrong or there is a typo.
can someone help me please?
Thank you in advance,
Norman
The INSERT statement has either the form:
INSERT INTO table (...) VALUES (...)
or:
INSERT INTO table (...) SELECT ... FROM ...
That's why Oracle issues an error message.
But there's more. You pass the ALTERNATENAMES string value to the stored procedure but need more data from the PLACES_TABLE. Furthermore, Oracle doesn't support stored procedure calls like this:
LOAD_ALTERNATE_NAMES(SELECT ALTERNATENAMES FROM PLACES_TABLE);
So I propose you create a stored procedure without parameters:
CREATE PROCEDURE LOAD_ALTERNATE_NAMES
AS
COMMA_FINDER NUMBER;
BEGIN
FOR REC IN (
SELECT * FROM PLACES_TABLE WHERE ALTERNATENAMES IS NOT NULL
) LOOP
COMMA_FINDER NUMBER := 1;
<<SEPARATE_ALTERNATENAMES>> WHILE COMMA_FINDER!=0 LOOP
INSERT INTO INFO_TABLE
(INFOID, NAME, LANGUAGE, STATUS, COUNTRY_CODE, COUNTRY_CODE_2, GID, SUPPLIERID, LAST_MODIFIED)
VALUES
(INFO_COUNTER.NEXTVAL, SUBSTR(REC.ALTERNATENAMES, INSTR(REC.ALTERNATENAMES, ',', COMMA_FINDER+1)), NULL, 'ALTERNATE', REC.COUNTRY_CODE, REC.COUNTRY_CODE_2, REC.GID, NULL, REC.LASTMODIFIED);
COMMA_FINDER := INSTR(REC.ALTERNATENAMES, ',', COMMA_FINDER);
END LOOP SEPARATE_ALTERNATENAMES;
END LOOP;
END
/
I hope that helps you proceed. I haven't test it and I'm afraid that SUBSTR will fail once it reaches the last name. But you'll figure that out.
Here is a little function I use to loop things like you are asking for. You can specify a delimiter.
The type...
type split_array is table of varchar2(32767) index by binary_integer;
The function...
function split(string_in varchar2, delim_in varchar2) return split_array is
i number :=0;
pos number :=0;
lv_str varchar2(32767) := string_in;
strings split_array;
dl number;
begin
-- determine first chuck of string
pos := instr(lv_str,delim_in,1,1);
-- get the length of the delimiter
dl := length(delim_in);
if (pos = 0) then --then we assume there is only 1 items in the list. so we just add the delimiter to the end which would make the pos length+1;
strings(1) := lv_str;
end if;
-- while there are chunks left, loop
while ( pos != 0) loop
-- increment counter
i := i + 1;
-- create array element for chuck of string
strings(i) := substr(lv_str,1,pos-1);
-- remove chunk from string
lv_str := substr(lv_str,pos+dl,length(lv_str));
-- determine next chunk
pos := instr(lv_str,delim_in,1,1);
-- no last chunk, add to array
if pos = 0 then
strings(i+1) := lv_str;
end if;
end loop;
-- return array
return strings;
end split;
How to use it...
declare
/* alternatenames varchar2(32767) := 'one,two,three,four'; */
nameArray split_array;
begin
for c1 in ( select alternatenames from yourTable where alternatenames is not null )
loop
nameArray := split(c1.alternatenames,',');
for i in 1..nameArray.count loop
/* dbms_output.put_line(nameArray(i)); */
insert into yourTable ( yourColumn ) values ( nameArray(i) );
end loop;
end loop;
end;
/

Oracle PLSQL: Help Required: Parsing String Collection or Concatenated String

Whenever the length of string l_long_string is above 4000 characters, the following code is throwing an error:
ORA-01460: unimplemented or unreasonable conversion requested
Instead of the nested regexp_substr query, when I try to use
SELECT column_value
FROM TABLE(l_string_coll)
it throws:
ORA-22905: cannot access rows from a non-nested table item
How can I modify the dynamic query?
Notes:
- l_string_coll is of type DBMS_SQL.VARCHAR2S, and comes as input to my procedure (here, i have just shown as an anonymous block)
- I'll have to manage without creating a User-defined Type in DB schema, so I am using the in-built DBMS_SQL.VARCHAR2S.
- This is not the actual business procedure, but is close to this. (Can't post the original)
- Dynamic query has to be there since I am using it for building the actual query with session, current application schema name etc.
/*
CREATE TABLE some_other_table
(word_id NUMBER(10), word_code VARCHAR2(30), word VARCHAR2(255));
INSERT INTO some_other_table VALUES (1, 'A', 'AB');
INSERT INTO some_other_table VALUES (2, 'B', 'BC');
INSERT INTO some_other_table VALUES (3, 'C', 'CD');
INSERT INTO some_other_table VALUES (4, 'D', 'DE');
COMMIT;
*/
DECLARE
l_word_count NUMBER(10) := 0;
l_counter NUMBER(10) := 0;
l_long_string VARCHAR2(30000) := NULL;
l_dyn_query VARCHAR2(30000) := NULL;
l_string_coll DBMS_SQL.VARCHAR2S;
BEGIN
-- l_string_coll of type DBMS_SQL.VARCHAR2S comes as Input to the procedure
FOR i IN 1 .. 4100
LOOP
l_counter := l_counter + 1;
l_string_coll(l_counter) := 'AB';
END LOOP;
-- Above input collection is concatenated into CSV string
FOR i IN l_string_coll.FIRST .. l_string_coll.LAST
LOOP
l_long_string := l_long_string || l_string_coll(i) || ', ';
END LOOP;
l_long_string := TRIM(',' FROM TRIM(l_long_string));
dbms_output.put_line('Length of l_long_string = ' || LENGTH(l_long_string));
/*
Some other tasks in PLSQL done successfully using the concatenated string l_long_string
*/
l_dyn_query := ' SELECT COUNT(*)
FROM some_other_table
WHERE word IN ( SELECT TRIM(REGEXP_SUBSTR(str, ''[^,]+'', 1, LEVEL)) word
FROM ( SELECT :string str FROM SYS.DUAL )
CONNECT BY TRIM(REGEXP_SUBSTR(str, ''[^,]+'', 1, LEVEL)) IS NOT NULL )';
--WHERE word IN ( SELECT column_value FROM TABLE(l_string_coll) )';
EXECUTE IMMEDIATE l_dyn_query INTO l_word_count USING l_long_string;
dbms_output.put_line('Word Count = ' || l_word_count);
EXCEPTION
WHEN OTHERS
THEN
dbms_output.put_line('SQLERRM = ' || SQLERRM);
dbms_output.put_line('FORMAT_ERROR_BAKCTRACE = ' || dbms_utility.format_error_backtrace);
END;
/
How can I modify the dynamic query?
First of all. Based on the code you've provided, there is absolutely no need to use dynamic, native or DBMS_SQL dynamic SQL at all.
Secondly, SQL cannot operate on "strings" that are greater than 4K bytes in length(Oracle versions prior to 12c), or 32K bytes(Oracle version 12cR1 and up, if MAX_STRING_SIZE initialization parameter is set to EXTENDED).
PL/SQL, on the other hand, allows you to work with varchar2() character strings that are greater than 4K bytes (up to 32Kb) in length. If you just need to count words in a comma separated sting, you can simply use regexp_count() regular expression function(Oracle 11gr1 and up) as follows:
set serveroutput on;
set feedback off;
clear screen;
declare
l_str varchar2(100) := 'aaa,bb,ccc,yyy';
l_numOfWords number;
begin
l_numOfWords := regexp_count(l_str, '[^,]+');
dbms_output.put('Number of words: ');
dbms_output.put_line(to_char(l_numOfWords));
end;
Result:
Number of words: 4

Resources