If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Temporary table creation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-02-10, 06:07
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Temporary table creation

I have read only access to my database. I am using the query that bit too complex or long.

Can i create temporary table even though i have read only access, I fso can provide me the syntax.
__________________
Vyas| Miracle Happens
Reply With Quote
  #2 (permalink)  
Old 06-02-10, 06:26
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
you can use the CTE in db2 ....

syntax will be

Quote:
with temp1 as (select.................)
select * from temp1
Reply With Quote
  #3 (permalink)  
Old 06-02-10, 06:50
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Red face Getting error with that

Error: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "M TABLE1)". Expected tokens may include: "<query_expr_body>". SQLSTATE=42601
(State:42601, Native Code: FFFFFF98)
__________________
Vyas| Miracle Happens
Reply With Quote
  #4 (permalink)  
Old 06-02-10, 07:15
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
can you paste me your query???
Reply With Quote
  #5 (permalink)  
Old 06-02-10, 07:42
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Here you go...

with temp1 AS (SELECT TM_ZN_DSCR FROM IBSPDM1A.TPDTM_ZN)
__________________
Vyas| Miracle Happens
Reply With Quote
  #6 (permalink)  
Old 06-02-10, 08:07
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
after that statement, you have to fetch the records .. this temporary table is valid only for that run alone.

your query needs to be like...

with temp1 as (SELECT TM_ZN_DSCR FROM IBSPDM1A.TPDTM_ZN)
select * from temp1
Reply With Quote
  #7 (permalink)  
Old 06-02-10, 08:15
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Thx..buddy..it's working...
__________________
Vyas| Miracle Happens
Reply With Quote
  #8 (permalink)  
Old 06-02-10, 09:06
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Can we use multiple temp table??

Hi As one temp table working but if say i wanna use two temp table

with temp1 as (select...)

with temp2(
select A.* from t1 a,table2 b
where a.id=b.id ) select * from temp2


Can we do like that?? i tried but getting some error.
__________________
Vyas| Miracle Happens
Reply With Quote
  #9 (permalink)  
Old 06-02-10, 09:51
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Close, but the WITH is only used once:
Code:
with temp1   as (select...
                      )
   , temp2   as (select A.* 
                 from t1 a
                    , table2 b
                 where a.id=b.id 
                ) 
select * from temp2
Reply With Quote
  #10 (permalink)  
Old 06-02-10, 10:19
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
cool man..
Reply With Quote
  #11 (permalink)  
Old 06-02-10, 11:51
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
With

WITH can increase clarity and improve queries by forcing a re-examining of the query, but a query rewritten using 'WITH' can still error out because it is too long or complex.

I had a query that was very complex. In my experience long case statements often create queries that fail. I replaced a failing long case by placing very simple case statements in a pre-query using with like the following:

Code:
with source as
select
field 1,
field 2,
case when field1 < somevalue then 0 else 1 end as flg1,
case when field2 > somevalue then 0 else 1 end as flg2
from tablename
where cond
This meant that my query logic was much simplified and it ran:
Code:
select
field1,
field2,
case 
when flg1 = 0 and flg2 = 0 then 1
when flg1 = 0 and flg2 = 1 then 2
when flg1 = 1 and flg2 = 0 then 3
when flg1 = 1 and flg2 = 1 then 4
end as mycasename
from source
The logic is much more exposed and it ran, but with does not guarantee the query will not be too complex.
Reply With Quote
  #12 (permalink)  
Old 06-02-10, 12:52
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Vyas, you might, also, have better luck if you tell us what problem you were having with the original query and perhaps run a corrected version of that, rather than adding complexity to it.
Dave
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On