0

My stored procedure is used for getting string values of ids separate by ',' from a string contains names in table AUTH_GROUPS.

for example:

id        name
1         role_1
2         role_2
3         role_3

the input is: 'role_1,role_2,role_3'

the output is: '1,2,3'

CREATE OR REPLACE PROCEDURE PROCEDURE1(P_ERROR_MSG OUT VARCHAR2, P_ROLE_STRING IN VARCHAR2 ) AS 
BEGIN
  DECLARE
 lvOutPut VARCHAR2(2000);
 vId varchar2(1000);
BEGIN
    lvOutPut := '';
  FOR i IN
    (SELECT trim(regexp_substr(P_ROLE_STRING, '[^,]+', 1, LEVEL)) l
    FROM dual
    CONNECT BY LEVEL <= regexp_count(P_ROLE_STRING, ',')+1
    )
  LOOP
    select id into vId from AUTH_GROUPS where NAME = i; --this line got error 'expression is of wrong type'
    lvOutPut := lvOutPut || vId || ',';
  END LOOP;
    P_ERROR_MSG := lvOutPut;
    P_ERROR_MSG := substr(P_ERROR_MSG, 1, LENGTH(P_ERROR_MSG) - 1);
END;
END PROCEDURE1;

But it has an error in the line that I commented. I tried i.1 or i.value but still got errors.

2 Answers 2

2

You need to use the actual column name. i is loop handle name.

....
BEGIN
    lvOutPut := '';
  FOR i IN
    (SELECT trim(regexp_substr(P_ROLE_STRING, '[^,]+', 1, LEVEL)) l -- this is column name to be used inside the loop
    FROM dual
    CONNECT BY LEVEL <= regexp_count(P_ROLE_STRING, ',')+1
    )
  LOOP
    select id into vId from AUTH_GROUPS where NAME = i.l;  -- change here
...
Sign up to request clarification or add additional context in comments.

Comments

0
/* Formatted on 6/23/2020 2:08:34 PM (QP5 v5.354) */
CREATE OR REPLACE PROCEDURE PROCEDURE1 (P_ERROR_MSG        OUT VARCHAR2,
                                        P_ROLE_STRING   IN     VARCHAR2)
AS
BEGIN
    DECLARE
        lvOutPut   VARCHAR2 (2000);
        vId        VARCHAR2 (1000);
    BEGIN
        lvOutPut := '';

        FOR i IN ( SELECT TRIM (REGEXP_SUBSTR (P_ROLE_STRING,
                                                  '[^,]+',
                                                  1,
                                                  LEVEL))    l
                        FROM DUAL
                  CONNECT BY LEVEL <= REGEXP_COUNT (P_ROLE_STRING, ',') + 1)
        LOOP
            SELECT id
              INTO vId
              FROM AUTH_GROUPS
             WHERE NAME = i.l; --this line got error 'expression is of wrong type'

            lvOutPut := lvOutPut || vId || ',';
        END LOOP;

        P_ERROR_MSG := lvOutPut;
        P_ERROR_MSG := SUBSTR (P_ERROR_MSG, 1, LENGTH (P_ERROR_MSG) - 1);
    END;
END PROCEDURE1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.