dbTalk Databases Forums  

Generating rows on the fly

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Generating rows on the fly in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
YONETANI Tomokazu
 
Posts: n/a

Default Generating rows on the fly - 04-25-2005 , 10:58 PM






Hi.
You can use the following SQL to construct rows with column names
on the fly, rather than from an existing table like sysibm.sysdummy1:
SELECT * FROM TABLE (
VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8)
) X(foo, bar, baz);
or
WITH X(foo, bar, baz) AS (
VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8)
) SELECT * FROM X;

As far as I tested, this construct is available on 7.2.8 and onwards.
This is sometimes useful, for example in MERGE INTO statement, or
when you don't want to create a table with a random name just to
prepare a few rows like above.

My question is, is this DB2-specific syntax, or is it(going to be)
a part of the standard in the future? I tried on PostgreSQL, but it
doesn't even accept the following SQL(to generate three columns
without column names):
VALUES (0, 1, 2);

Thanks in advance.


Reply With Quote
  #2  
Old   
Knut Stolze
 
Posts: n/a

Default Re: Generating rows on the fly - 04-26-2005 , 06:26 AM






YONETANI Tomokazu wrote:

Quote:
Hi.
You can use the following SQL to construct rows with column names
on the fly, rather than from an existing table like sysibm.sysdummy1:
SELECT * FROM TABLE (
VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8)
) X(foo, bar, baz);
or
WITH X(foo, bar, baz) AS (
VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8)
) SELECT * FROM X;

As far as I tested, this construct is available on 7.2.8 and onwards.
This is sometimes useful, for example in MERGE INTO statement, or
when you don't want to create a table with a random name just to
prepare a few rows like above.

My question is, is this DB2-specific syntax, or is it(going to be)
a part of the standard in the future? I tried on PostgreSQL, but it
doesn't even accept the following SQL(to generate three columns
without column names):
VALUES (0, 1, 2);
That is standardized SQL for many years already. See clause 7.3, <table
value constructor> in the SQL99 standard.

As to the reasons why PostgreSQL doesn't support this syntax, you might want
to ask the developers there.

p.s: The SYSIBM.SYSDUMMY1 catalog view is actually defined in such a way.
"create view sysibm.sysdummy1 (ibmreqd) as values (char('Y'))" (Extracted
using "SELECT text FROM syscat.views WHERE viewname = 'SYSDUMMY1'".)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena


Reply With Quote
  #3  
Old   
Knut Stolze
 
Posts: n/a

Default Re: Generating rows on the fly - 04-30-2005 , 12:31 AM



--CELKO-- wrote:

Quote:
I do not remember LATERAL as a keyword, but you can write:
Have a look at Subclause 7.6, <table reference> in SQL2003. This is feature
F491.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena


Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: Generating rows on the fly - 04-30-2005 , 01:56 PM



I am honestly lost on this one. I would expand and qualify that example
like this:

SELECT * FROM T
WHERE EXISTS
(SELECT *
FROM T AS T1
CROSS JOIN
(SELECT 1
FROM S
WHERE S.PK = T1.PK)
AS X(i)
WHERE T1.C1 = X.i) ;

I just follow the usual scoping rules to find the innermost copy of
table T. if I want some strange realtionships, I cn qualify the copies
of T

SELECT * FROM T AS T0
WHERE EXISTS
(SELECT *
FROM T AS T1
CROSS JOIN
(SELECT 1
FROM S
WHERE S.PK = T0.PK)
AS X(i)
WHERE T1.C1 = X.i) ;

What does LATERAL do that is different?

Quote:
According to the bedtime stories I heard ...
The keyword COALESCE() instead of IBM's VALUE() is another story like
that. Someone at the meeting had a pocket thesaurus and pulled that
one out of list of things like "melt", "fuse", "meld", etc. It ws the
least likely to be a column or table name, so we went with it



Reply With Quote
  #5  
Old   
Serge Rielau
 
Posts: n/a

Default Re: Generating rows on the fly - 04-30-2005 , 03:18 PM



--CELKO-- wrote:
Quote:
I am honestly lost on this one. I would expand and qualify that example
like this:

SELECT * FROM T
WHERE EXISTS
(SELECT *
FROM T AS T1
CROSS JOIN
(SELECT 1
FROM S
WHERE S.PK = T1.PK)
AS X(i)
WHERE T1.C1 = X.i) ;
This is, strictly speaking, illegal. The subquery cannot see T1.PK.
In DB2 you will get a resolution error.
If, however, you wrap LATERAL (or TABLE) around the query:
SELECT * FROM T
WHERE EXISTS
(SELECT *
FROM T AS T1
CROSS JOIN
LATERAL(SELECT 1
FROM S
WHERE S.PK = T1.PK)
AS X(i)
WHERE T1.C1 = X.i) ;

T1.PK magically moves into view.

Quote:
I just follow the usual scoping rules to find the innermost copy of
table T. if I want some strange realtionships, I cn qualify the copies
of T

SELECT * FROM T AS T0
WHERE EXISTS
(SELECT *
FROM T AS T1
CROSS JOIN
(SELECT 1
FROM S
WHERE S.PK = T0.PK)
AS X(i)
WHERE T1.C1 = X.i) ;

What does LATERAL do that is different?


According to the bedtime stories I heard ...


The keyword COALESCE() instead of IBM's VALUE() is another story like
that. Someone at the meeting had a pocket thesaurus and pulled that
one out of list of things like "melt", "fuse", "meld", etc. It ws the
least likely to be a column or table name, so we went with it
I wonder whether there is a market for a story book on the SQL Standard,
or standards in general...

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab


Reply With Quote
  #6  
Old   
Knut Stolze
 
Posts: n/a

Default Re: Generating rows on the fly - 05-02-2005 , 05:26 AM



Serge Rielau wrote:

Quote:
The keyword COALESCE() instead of IBM's VALUE() is another story like
that. Someone at the meeting had a pocket thesaurus and pulled that
one out of list of things like "melt", "fuse", "meld", etc. It ws the
least likely to be a column or table name, so we went with it
I wonder whether there is a market for a story book on the SQL Standard,
or standards in general...
My experience is that people are indeed interested in this kind of
stories. ;-)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena


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 - 2013, Jelsoft Enterprises Ltd.