简体   繁体   中英

Select data from dual table

I have a data as following: 1203 1222 3201 4300 Which are numbers of products, so i need to select them as a data in table .. I think the solution will be with using dual table .. I tried the following code:

Select * from { values '1203','1222','3201','4300'} as Table1 

But it not working !

Oracle has the TABLE function, which takes as input a varray and returns an anonymous table (anonymous means "the table doesn't have a name"), with a single column named column_value .

Oracle also has the function sys.odcinumberlist() , which takes as input a comma-separated list of numbers and returns a varray of numbers. (Similarly, to generate a varray of varchar2 values, there is sys.odcivarchar2list() .)

To input your for values and return a result with a single column named num (or any other name you want), you can use these two functions together - and alias the column in the select clause. Like this:

select column_value as num
from   table( sys.odcinumberlist( 1203, 1222, 3201, 4300 ) )
;

   NUM
  ----
  1203
  1222
  3201
  4300

In standard SQL you would need to use:

Select * 
from (values ('1203','1222','3201','4300')) as Table1;

That would create a single row with four (character) columns. If you intended to return four rows it would be:

Select * 
from (values ('1203'),('1222'),('3201'),('4300')) as Table1 

However, Oracle does NOT support that syntax. Neither does it support a SELECT without a FROM clause.

If you want a single row with 4 columns in Oracle you need

select '1203','1222','3201','4300'
from dual;

If you want four rows you need

select '1203'
from dual
union all
select '1222'
from dual
union all
select '3201'
from dual
union all
select '4300'
from dual;

Additionally: '1203' is not a "number". It's a character string. Number literals are written without quotes in SQL: 1203 is a number

You can also use XMLTABLE .

select TO_NUMBER(TRIM(COLUMN_VALUE)) as numbers from  XMLTABLE( '1203,1222,3201,4300');

Numbers
-------
1203
1222
3201
4300

This works only for numbers.

EDIT: Better to wrap it with to_number as suggested by experts in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM