Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Invalid Number in an IN list

Catfive LanderJun 25 2009 — edited Jun 28 2009
This SQL statement works fine:
update catalogue_table set keywords = keywords||','||trim(lower('someword'))||',' where recid in (6037,6038,6039,6040);
But I want users to be able to supply the new keyword and the identifiers of the records they want updated, dynamically. So I write a procedure. This version also works OK:
create or replace PROCEDURE keyword_add (p_recid_list IN VARCHAR2,
                       p_keyword IN VARCHAR2)
IS
  v_keyword varchar2(64);
  v_sql varchar2(2000);

BEGIN
  v_keyword := ','||lower(trim(p_keyword))||',';  
  v_sql := 'update catalogue_table set keywords = keywords||'''||v_keyword||''' where recid in ('||p_recid_list||')';
  execute immediate v_sql;  
  commit;
END; 
The user calls the procedure like so:
begin
  keyword_add(p_recid_list => '6037,6038,6039,6040',
                            p_keyword => 'someword');
end; 
...and all works well. But I then get to thinking that I would prefer not to use dynamic SQL unless it's really called for, so I rewrite the code like this
create or replace PROCEDURE keyword_add (p_recid_list IN VARCHAR2,
                       p_keyword IN VARCHAR2)
IS
  v_keyword varchar2(64);

BEGIN
  v_keyword := ','||lower(trim(p_keyword))||',';  
  update catalogue_table set keywords = keywords||v_keyword where recid_id in (p_recid_list);
  commit;
END; 
...and this time, using the exact same "calling code" as before, I simply get an ORA-01722 error ('invalid number'). Well, I know that the string '6037,6038,6039,6040' isn't a valid number, but it's not supposed to be! It's supposed to be a string list of numbers that are used as the basis of an IN comparison. The standalone SQL version works just fine with such a list being used to find numeric recids. Why won't my procedure's code work the same, please??

Any help in getting me to understand what I'm doing wrong would be much appreciated!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 26 2009
Added on Jun 25 2009
48 comments
2,378 views