Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

Update using CSV in a vachar

Hi all,

I have a stored procedure that accepts comma separated value in varchar2 variable. I basically want to update all of the records that this csv value holds so for example something like this...

Is this possible? or my only option is to loop through each csv value and then update each at a time? When I try below code out in a stored procedure I get a
ORA-01722: invalid number error.

PROCEDURE QueueStudies
(
      csv_rids      IN Varchar2            -- Comma separated RID Values
)
AS
BEGIN    
       UPDATE sometable
      SET col5 = 'NO',
      col4 = '',
      col3 = 'NO',
      col2 = '',
      col1 = 'YES'
      where rid in (csv_rids);
END QueueStudies;
0
darshita00
Asked:
darshita00
1 Solution
 
sventhanCommented:
darshita -
Try string to table fn as described in the link
http://www.oracle.com/technology/oramag/oracle/07-mar/o27asktom.html 
0
 
sdstuberCommented:
the problem is an "in" clause isn't a string.

so
you query is effectively this...

if csv_rids is '1,2,3,4'

 UPDATE sometable
      SET col5 = 'NO',
      col4 = '',
      col3 = 'NO',
      col2 = '',
      col1 = 'YES'
      where rid in ('1,2,3,4');   -- in clause on 1 string

which is NOT the same as


UPDATE sometable
      SET col5 = 'NO',
      col4 = '',
      col3 = 'NO',
      col2 = '',
      col1 = 'YES'
      where rid in (1,2,3,4);  -- in clause on 4 numbers

0
 
sdstuberCommented:
and, yes, you need the str2tbl function as indicated above
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
johnsoneSenior Oracle DBACommented:
You would either have to loop through the values or use execute immediate (see example).

Are these numeric keys?  If so, this method should work.  If they are anything other than numeric keys, you need to make sure that the comma separated list has the proper quotation marks.
PROCEDURE QueueStudies
(
      csv_rids      IN Varchar2            -- Comma separated RID Values
)
AS
BEGIN    
      EXECUTE IMMEDIATE 'UPDATE sometable SET col5 = ''NO'', col4 = null, col3 = ''NO'', col2 = NULL, col1 = ''YES'' where rid in (' || csv_rids || ')';
END QueueStudies;

Open in new window

0
 
sujith80Commented:
How frequently do you execute the code?
If it is an "occasionally" executed program, you may try to use dynamic sql.
(bit of a mess with the single quotes, but will do)
PROCEDURE QueueStudies
(
      csv_rids      IN Varchar2            -- Comma separated RID Values
)
AS
BEGIN     
 execute immediate 	
 	'UPDATE sometable '||
 	'SET col5 = ''NO'' , '||
      	'col4 = null , '||
      	'col3 = ''NO'' , '||
      	'col2 = null , '||
      	'col1 = ''YES'' ||
      	'where rid in ('||csv_rids||')';
END QueueStudies;

Open in new window

0
 
sdstuberCommented:
I think a split is in order, or give sventhan all of the points.

I explained what the problem was but sventhan pointed you to a solution (actually several of them)

The other posts could work too, but I don't recommend those.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now