dbTalk Databases Forums  

Several rows from Dual

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Several rows from Dual in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Hans Mayr
 
Posts: n/a

Default Several rows from Dual - 02-12-2009 , 11:33 AM






Hello,

for some reason I need something like a "select 123 as abc from dual;"
but with several result rows rather than one. The result should look
like

ABC
123
456
789

So I came up with the following solution which seems akward to me:

SELECT
abc
FROM
(SELECT level abc FROM dual CONNECT BY level <= 999
)
WHERE
abc IN(123, 456, 789) ;

It was essential to me that I could easyly change the number of
results like here through just writing them between the brackets,
otherwise I could have used a UNION statement. But my solution looks
ugly, especially the "<=999" because I will need it for bigger than 3
digit numbers. Does anybody know a better solution?

Thanks and best,

Hans

Reply With Quote
  #2  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: Several rows from Dual - 02-12-2009 , 12:33 PM






Hans Mayr schrieb:
Quote:
Hello,

for some reason I need something like a "select 123 as abc from dual;"
but with several result rows rather than one. The result should look
like

ABC
123
456
789

So I came up with the following solution which seems akward to me:

SELECT
abc
FROM
(SELECT level abc FROM dual CONNECT BY level <= 999
)
WHERE
abc IN(123, 456, 789) ;

It was essential to me that I could easyly change the number of
results like here through just writing them between the brackets,
otherwise I could have used a UNION statement. But my solution looks
ugly, especially the "<=999" because I will need it for bigger than 3
digit numbers. Does anybody know a better solution?

Thanks and best,

Hans
It seems to be not very efficient to generate tousend rows if you know a
priori, you'll need only 3 from this rowset, so i would probably do a
union all.
If you have a possibility to create helper types, then ( i often seen it
in William Robertson's examples and like this approach), you can do
something like

SQL> create or replace type number_t is table of number
2 ;
3 /

Type created.

SQL>
SQL> select *
2 from table(number_t(123,456,789))
3 ;

COLUMN_VALUE
------------
123
456
789


Best regards

Maxim


Reply With Quote
  #3  
Old   
Hans Mayr
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 02:56 AM



Hi Maxim,

Thanks. This is an elegant solution. However I wonder if there is a
possibility which does not involve creating anything, just a plain
"one sql statement solution" ...

Best,

Hans

Reply With Quote
  #4  
Old   
Joel
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 05:02 AM



On Feb 13, 3:56*am, Hans Mayr <mayr1... (AT) gmx (DOT) de> wrote:
Quote:
Hi Maxim,

Thanks. This is an elegant solution. However I wonder if there is a
possibility which does not involve creating anything, just a plain
"one sql statement solution" ...

Best,

Hans
does this count as a one sql statement solution?

select 123 as abc from dual
union
select 456 from dual
union
select 789 from dual;


Reply With Quote
  #5  
Old   
Hans Mayr
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 07:45 AM



Hi Joel!

Quote:
does this count as a one sql statement solution?

select 123 as abc from dual
union
select 456 from dual
union
select 789 from dual;
I explicitly stated that I know that union could have been used and
that I was looking for a solution that is easy to adapt from 1 to n
rows. Don't misunderstand me, at this point I am not looking for "a"
solution anymore, both my method and Maxim's solution work fine and
yours would work, too, though it is less flexible. I just would like
to learn more about sophisticated solutions regarding my problem to
let me understand (Oracle) SQL better.

Best,

Hans


Reply With Quote
  #6  
Old   
Mark D Powell
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 08:47 AM



On Feb 13, 8:45*am, Hans Mayr <mayr1... (AT) gmx (DOT) de> wrote:
Quote:
Hi Joel!

does this count as a one sql statement solution?

select 123 as abc from dual
union
select 456 from dual
union
select 789 from dual;

I explicitly stated that I know that union could have been used and
that I was looking for a solution that is easy to adapt from 1 to n
rows. Don't misunderstand me, at this point I am not looking for "a"
solution anymore, both my method and Maxim's solution work fine and
yours would work, too, though it is less flexible. I just would like
to learn more about sophisticated solutions regarding my problem to
let me understand (Oracle) SQL better.

Best,

Hans
A global temporary table might be a solution for you. You would
define the definition once then each program that referenced the table
could execute a procedure or chunk of code to insert the values to be
used in this run.

Then subsequent SQL statements could reference the GTT to get the
values. Because each session gets its own copy of the GTT multiple
processes could run concurrently with different sets of values.

If the application is web based then you might have to use a permanent
table of tables structure where you add an identifier that the end
user session uses to identify its list of values.

The best solution depends on exactly why you need the list, how the
list of values is determined, and how it is used. Maxim's solution
sounded from your posts that it will do. I never remember to consider
solutions like that but there are often multiple approaches and the
best needs to be determined based on the requirements on hand.

HTH -- Mark D Powell --



Reply With Quote
  #7  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 03:21 PM



On 13.02.2009 15:47, Mark D Powell wrote:
Quote:
On Feb 13, 8:45 am, Hans Mayr <mayr1... (AT) gmx (DOT) de> wrote:
Hi Joel!

does this count as a one sql statement solution?
select 123 as abc from dual
union
select 456 from dual
union
select 789 from dual;
I explicitly stated that I know that union could have been used and
that I was looking for a solution that is easy to adapt from 1 to n
rows. Don't misunderstand me, at this point I am not looking for "a"
solution anymore, both my method and Maxim's solution work fine and
yours would work, too, though it is less flexible. I just would like
to learn more about sophisticated solutions regarding my problem to
let me understand (Oracle) SQL better.

Best,

Hans

A global temporary table might be a solution for you. You would
define the definition once then each program that referenced the table
could execute a procedure or chunk of code to insert the values to be
used in this run.

Then subsequent SQL statements could reference the GTT to get the
values. Because each session gets its own copy of the GTT multiple
processes could run concurrently with different sets of values.

If the application is web based then you might have to use a permanent
table of tables structure where you add an identifier that the end
user session uses to identify its list of values.

The best solution depends on exactly why you need the list, how the
list of values is determined, and how it is used. Maxim's solution
sounded from your posts that it will do. I never remember to consider
solutions like that but there are often multiple approaches and the
best needs to be determined based on the requirements on hand.
IMHO this is the most important question. A query returning three
distinct numeric values is worthless in itself. Hans, what do you need
that for?

Cheers

robert


Reply With Quote
  #8  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: Several rows from Dual - 02-13-2009 , 03:43 PM



Hans Mayr schrieb:
Quote:
Hi Joel!

does this count as a one sql statement solution?

select 123 as abc from dual
union
select 456 from dual
union
select 789 from dual;

I explicitly stated that I know that union could have been used and
that I was looking for a solution that is easy to adapt from 1 to n
rows. Don't misunderstand me, at this point I am not looking for "a"
solution anymore, both my method and Maxim's solution work fine and
yours would work, too, though it is less flexible. I just would like
to learn more about sophisticated solutions regarding my problem to
let me understand (Oracle) SQL better.

Best,

Hans
If you are simply looking for different techniques to solve that task,
besides populating a global temporary table as suggested by Mark, here 2
another alternatives with plain sql. You could as well achieve that with
built in xml functions ( i mean primarily xquery/xml table), but being
probably most flexible, they would probably perform worst compared to
other approaches.

SQL> select r
2 from dual
3 model return updated rows
4 dimension by (1 r)
5 measures (cast (null as number) n)
6 rules
7 (
8 n[for r in (123,456,789,42)]=null
9 )
10 ;

R
----------
123
456
789
42

SQL>
SQL> with t as (
2 select '123,456,789,42' s from dual
3 )
4 select regexp_substr(s,'\w+',1,level)
5 from t
6 connect by regexp_substr(s,'\w+',1,level)
7 is not null
8 ;

REGEXP_SUBSTR(S,'\W+',1,LEVEL)
--------------------------------------------------------
123
456
789
42

If you are looking in general for good examples of sql/plsql, in my
opinion good places are

http://www.oracle-developer.net/index.php
http://www.williamrobertson.net/
http://www.sqlsnippets.com/en/home.html

Best regards

Maxim


Reply With Quote
  #9  
Old   
Hans Mayr
 
Posts: n/a

Default Re: Several rows from Dual - 02-16-2009 , 02:58 AM



Thanks Maxim! Another great answer.

Robert: The context is as follows: A user analyses a couple of items.
This analysis has to be done manually. For several items (ids
123,456,789) he identified one has to perform an insert. I was looking
for an easy way to perform this insert so I take the result of the
query above, do a join on it which returns the rows to be inserted.
The point was to have a single point in the insert sql which had to be
changed to do the operation.

Best,

Hans

Reply With Quote
  #10  
Old   
William Robertson
 
Posts: n/a

Default Re: Several rows from Dual - 02-17-2009 , 03:44 AM



On Feb 13, 8:56*am, Hans Mayr <mayr1... (AT) gmx (DOT) de> wrote:
Quote:
Hi Maxim,

Thanks. This is an elegant solution. However I wonder if there is a
possibility which does not involve creating anything, just a plain
"one sql statement solution" ...

Best,

Hans
If you don't want to create a type, you could just use one that
already exists. Query all_coll_types where coll_type = 'TABLE'
and elem_type_name is 'INTEGER' or 'NUMBER' and take your pick. How
about ku$_objnumset? (It's used in DBMS_METADATA.)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.