0

This may be very basic. I am very beginner of PL/SQL, but I am stuck with this issue. If somebody know the solution, please let me know.

This code

DECLARE      
  v_objectID VARCHAR2(100);
  v_account  VARCHAR2(100);

BEGIN           
     v_objectID :='21,22';

     DBMS_OUTPUT.PUT_LINE(v_objectID); 
END;
/

Output is

21,22

Then,

This code

DECLARE      
  v_objectID VARCHAR2(100);
  v_account  VARCHAR2(100);

BEGIN           



     SELECT LISTAGG(x.ACCOUNT, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT
     INTO v_account           
     FROM acctx x
     where x.OBJECT_ID IN (21,22);

     DBMS_OUTPUT.PUT_LINE(v_account); 
END;
/

OUTPUT is

1001,2002

Then I try to do like this

DECLARE      
  v_objectID VARCHAR2(100);
  v_account  VARCHAR2(100);

BEGIN           
     v_objectID :='21,22';


     SELECT LISTAGG(x.ACCOUNT, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT
     INTO v_account           
     FROM acctx x
     where x.OBJECT_ID IN (v_objectID);

     DBMS_OUTPUT.PUT_LINE(v_account); 
END;
/

I added v_objectID :='21,22';; This is causing the problem

The error is

ORA_07122: Invalid number ORA-06512: at line 9

How should I assign variable appropriately to output 1001,2002?

Thanks

1 Answer 1

1

The error is obvious, In your table object_id would have been of Number Datatype. Now you are trying to compare a number with a varchar, so you faced the issue. Try below:

DECLARE      
  v_objectID VARCHAR2(100);
  v_account  VARCHAR2(100);

BEGIN           
     v_objectID :='21,22';


     SELECT LISTAGG(x.A, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT
     INTO v_account           
     FROM test x
     where to_char(x.A) IN (v_objectID);

     DBMS_OUTPUT.PUT_LINE(v_account); 
END;
/
Sign up to request clarification or add additional context in comments.

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.