Solved

Update using CSV in a vachar

Posted on 2008-09-29
6
411 Views
Last Modified: 2013-12-18
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
Comment
Question by:darshita00
6 Comments
 
LVL 18

Expert Comment

by:sventhan
ID: 22597486
darshita -
Try string to table fn as described in the link
http://www.oracle.com/technology/oramag/oracle/07-mar/o27asktom.html 
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 50 total points
ID: 22597491
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
 
LVL 73

Expert Comment

by:sdstuber
ID: 22597498
and, yes, you need the str2tbl function as indicated above
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

by:johnsone
ID: 22597523
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
 
LVL 27

Expert Comment

by:sujith80
ID: 22597579
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
 
LVL 73

Expert Comment

by:sdstuber
ID: 22598019
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 73
1 FROM DUAL wont work with additional columns ?? 4 36
PL SQL Search Across Columns 4 35
Creation date for a PDB 5 18
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now