TransWikia.com

Avoid select from dual Union in PL/SQL

Stack Overflow Asked by ALC on December 30, 2021

i have this data:

 'DJORDAN','ADAVIS','MJONES','DPRESTOM','PSMITH'

I need convert the data to a cursor o in others cases using to a INSERT
Then i do:

  Select 'DJORDAN' User_Code from dual 
  union
  Select 'ADAVIS' User_Code from dual 
  union
  Select 'MJONES' User_Code  from dual 
  union
  Select 'DPRESTOM' User_Code  from dual 
  union
  Select 'PSMITH' User_Code from dual ;

Are there any alternative to Union DUal for this purpose?

Thanks in advance!

3 Answers

Yet another simple solution:

create table t1 (user_code varchar2 (10))
/
begin
    insert into t1 (user_code) 
    select trim (column_value) user_code 
    from xmlTable (
        replace (q'"'DJORDAN','ADAVIS','MJONES','DPRESTOM','PSMITH'"', '''', '"'));
end;
/
select * from t1;

Result:

USER_CODE
----------
DJORDAN
ADAVIS
MJONES
DPRESTOM
PSMITH

Answered by 0xdb on December 30, 2021

Alternatively, with quite a lot more typing, assuming that list of names is - actually - a string, you could split it to rows and then insert into the table. Something like this:

SQL> create table test (col varchar2(20));

Table created.

SQL> insert into test
  2  with temp (col) as
  3    (select q'['DJORDAN','ADAVIS','MJONES','DPRESTOM','PSMITH']' from dual)
  4  select regexp_substr(replace(col, chr(39), null), '[^,]+', 1, level) val
  5  from temp
  6  connect by level <= regexp_count(col, ',') + 1;

5 rows created.

SQL> select * from test;

COL
--------------------
DJORDAN
ADAVIS
MJONES
DPRESTOM
PSMITH

SQL>

Or, if you use Apex, its APEX_STRING.SPLIT also does the job:

select column_value
from apex_string.split('DJORDAN,ADAVIS,MJONES,DPRESTOM,PSMITH', ',');

Answered by Littlefoot on December 30, 2021

You can use a collection, for example:

select * from SYS.DBMS_DEBUG_VC2COLL ('DJORDAN','ADAVIS','MJONES','DPRESTOM','PSMITH');

Or for older versions of Oracle:

select * from TABLE(SYS.DBMS_DEBUG_VC2COLL ('DJORDAN','ADAVIS','MJONES','DPRESTOM','PSMITH'));

Answered by Tony Andrews on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP