Hello guys,
When i use the common-table-expression keyword WITH, where does the defined data reside? or DB2 just replaces the WITH table with the sql?
for example
with mytable as (select * from sysibm)
Select * from mytable
In DB2 v10, is there an easy way (like in oracle) to define father-child reletionship? or it is just a recursive function with the WITH clause.
Thanks alot,
P.S
Anyone encountered -725 sql error while rebinding the DRDA protocol?
Question about common table expression and father-child
-
KofKof
- Posts: 1
- Joined: Tue Nov 20, 2012 9:01 pm
- Skillset: DB2 3 years
- Referer: mainframeforumes.com
-
engh
- Posts: 18
- Joined: Fri Jul 08, 2022 7:29 pm
- Skillset: leblesmoutilabiskouti
- Referer: leblesmoutilabiskouti
Re: Question about common table expression and father-child
This is an old question, but I'm going to answer it anyway, because many people still ask themselves the same question today.
A Common Table Expression (CTE) is simply an externalized derived table.
For example, instead of writing :
you may write :
the advantages are :
It is also important to know that CTEs are the only solution for writing recursive queries.
Because CTE is only a derived table, there are no specific storage consideration. Data are only stored in the tables and buffered in RAM as usual.
A Common Table Expression (CTE) is simply an externalized derived table.
For example, instead of writing :
Code: Select all
select Col1
, Col2
, [...]
, Coln
from (select ColA as Col1
, ColB as Col2
, [...]
, ColX as Coln
from My_view
) as Derived_tableyou may write :
Code: Select all
with Derived_table as
(select ColA as Col1
, ColB as Col2
, [...]
, ColX as Coln
from My_view
)
select Col1
, Col2
, [...]
, Coln
from Derived_tablethe advantages are :
- the possible mutualization of the derived table when it is used several times in the same query
- the simplification of the writing of the main query
It is also important to know that CTEs are the only solution for writing recursive queries.
Because CTE is only a derived table, there are no specific storage consideration. Data are only stored in the tables and buffered in RAM as usual.
- sergeyken
- Posts: 473
- Joined: Wed Jul 24, 2019 10:12 pm
- Skillset: Assembler, JCL, Utilities, PL/I, C/C++, DB2, SQL, REXX, COBOL, etc. etc. etc.
- Referer: Internet search
Re: Question about common table expression and father-child
CTE from WITH clause can be considered as "local VIEW definition".
It works almost in the same manner as regular VIEW, but it only exists during the query execution where its corresponding WITH clause located.
It works almost in the same manner as regular VIEW, but it only exists during the query execution where its corresponding WITH clause located.
Javas and Pythons come and go, but JCL and SORT stay forever.
-
engh
- Posts: 18
- Joined: Fri Jul 08, 2022 7:29 pm
- Skillset: leblesmoutilabiskouti
- Referer: leblesmoutilabiskouti
Re: Question about common table expression and father-child
sergeyken wrote:CTE from WITH clause can be considered as "local VIEW definition".
It works almost in the same manner as regular VIEW, but it only exists during the query execution where its corresponding WITH clause located.
Granted, but let's not forget that:
- a view is an object declared in the relational catalog;
- as such, the view can be associated with specific privileges.
Conversely, the CTE is not declared in the relational catalog, and its privileges are those of the underlying objects.
-
- Similar Topics
- Replies
- Views
- Last post
-
- 12
- 9484
-
by sergeyken
View the latest post
Wed Jan 29, 2025 8:49 pm
-
-
FORMAT THE FILE SO ALL CHILD UNDER SAME PARENT IN ONE LINE
by azhar » Wed May 31, 2023 4:27 pm » in JCL - 1
- 2933
-
by sergeyken
View the latest post
Thu Jun 01, 2023 3:06 am
-
-
- 1
- 2022
-
by cobol_dev
View the latest post
Thu Apr 14, 2022 12:55 am
-
- 2
- 4336
-
by Pedro
View the latest post
Tue Feb 27, 2024 4:44 am
-
-
strategy: partitioned table, delete millions of rows
by BobHarris » Wed Aug 17, 2022 2:47 pm » in DB2 - 1
- 2923
-
by engh
View the latest post
Tue Sep 20, 2022 1:56 pm
-