Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select from list of values in Oracle

I am referring to this stackoverflow answer:

How can I select from list of values in SQL Server

How could something similar be done in Oracle?

I've seen the other answers on this page that use UNION and although this method technically works, it's not what I would like to use in my case.

So I would like to stay with syntax that more or less looks like a comma-separated list of values.

UPDATE regarding the create type table answer:

I have a table:

CREATE TABLE BOOK (   "BOOK_ID" NUMBER(38,0) ) 

I use this script but it does not insert any rows to the BOOK table:

create type number_tab is table of number;  INSERT INTO BOOK (     BOOK_ID ) SELECT A.NOTEBOOK_ID FROM     (select column_value AS NOTEBOOK_ID from table (number_tab(1,2,3,4,5,6))) A ; 

Script output:

TYPE number_tab compiled Warning: execution completed with warning 

But if I use this script it does insert new rows to the BOOK table:

INSERT INTO BOOK (     BOOK_ID ) SELECT A.NOTEBOOK_ID FROM     (SELECT (LEVEL-1)+1 AS NOTEBOOK_ID FROM DUAL CONNECT BY LEVEL<=6) A ; 
like image 351
rapt Avatar asked Apr 27 '12 15:04

rapt


People also ask

What is SELECT * from table in Oracle?

In short, it is used to convert a collection or pipelined function into a table that can be queried by a SELECT statement. Typically, the collection must be of a datatype that is defined at the database level (i.e. a datatype that was created by a create or replace type ... statement). e.g.

How do I write multiple SELECT statements in Oracle?

The SELECT INTO clause is used to retrieve one row or set of columns. It is used to store the returned data into predefined variables. For multiple SELECTs you can have multiple SELECT INTO clause, each clause would store the result of respective SQL. To return multiple rows, you could use CURSOR .

What is CHR 10 in Oracle?

CHR(10) -- Line feed. CHR(13) -- Carriage return. You can use them in insert like this, INSERT INTO table_name (columne_name) VALUES ('ABC' || CHR (9) || 'DEF' || CHR (10) || 'GHI' || CHR (13) || 'JKL') Here is the complete list of ascii values.


1 Answers

You don't need to create any stored types, you can evaluate Oracle's built-in collection types.

select distinct column_value from table(sys.odcinumberlist(1,1,2,3,3,4,4,5)) 
like image 198
Kirill Leontev Avatar answered Oct 21 '22 10:10

Kirill Leontev