Solved

DB2: V5R4M0: Create view and WITH statment

Posted on 2011-03-09
8
774 Views
Last Modified: 2012-05-11
Is it possible use WITH statment in a create view statment?
because the code below doesn't work with WITH...

thanks

CREATE VIEW QS36F/MATERIAL1 AS                                  
( WITH AAA AS ( SELECT HVARI2 AS MATERIAL                        
 FROM QS36F/DDSH001                                              
WHERE SUBSTRING(HVARI2, 1, 3) = '   ' AND TRIM(HVARI2)<>'' AND    
HVARI1                                                            
IN (SELECT GRoUP1 FROM QS36F/SGRoup) GROUP BY HVARI2 )          
SELECT AAA.*, MATDES.MATDES FROM AAA LEFT JOIN QS36F/MATDES ON    
MATERIAL=MATERI                                                  
)                                                                
0
Comment
Question by:bobdylan75
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 35083943
Hi Bob,

I'm not sure, but if it is you'll need to turn it around...


with AAA as (...)
CREATE VIEW myview ...


Kent
0
 

Author Comment

by:bobdylan75
ID: 35083983
thank you but, it doesn't work in this way...
I tried 1 minute ago...
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 35084042
why not

CREATE VIEW QS36F/MATERIAL1 AS                                  
(
           
SELECT AAA.*, MATDES.MATDES FROM
( SELECT HVARI2 AS MATERIAL                        
 FROM QS36F/DDSH001                                              
WHERE SUBSTRING(HVARI2, 1, 3) = '   ' AND TRIM(HVARI2)<>'' AND    
HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup) GROUP BY HVARI2 ) AAA
 LEFT JOIN QS36F/MATDES ON    
MATERIAL=MATERI                                                  
)            
0
 
LVL 18

Expert Comment

by:daveslash
ID: 35084046

Try removing the out parentheses.

e.g.

CREATE VIEW QS36F/MATERIAL1 AS                                   
  WITH AAA AS ( 
    SELECT HVARI2 AS MATERIAL
      FROM QS36F/DDSH001
     WHERE SUBSTRING(HVARI2, 1, 3) = '   '
       AND TRIM(HVARI2)<>'' 
       AND HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup) 
     GROUP BY HVARI2
  )
  SELECT AAA.*, 
         MATDES.MATDES 
    FROM AAA 
    LEFT JOIN QS36F/MATDES
      ON MATERIAL=MATERI

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Expert Comment

by:Kdo
ID: 35084060
Ah... You're trying to put the CTE into the view.  (I thought that you were trying to define the columns from the CTE.)

I don't believe that this is supported.  You can still build a view over the joined tables but not with the CTE syntax.

CREATE VIEW QS36F/MATERIAL1 AS                                  
SELECT AAA.*, MATDES.MATDES
FROM
(
  SELECT HVARI2 AS MATERIAL                        
  FROM QS36F/DDSH001                                              
  WHERE SUBSTRING(HVARI2, 1, 3) = '   '
    AND TRIM(HVARI2)<>''
    AND HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup)
  GROUP BY HVARI2
) AAA
LEFT JOIN QS36F/MATDES
  ON MATERIAL=MATERI                                                  
 


Kent
0
 
LVL 18

Expert Comment

by:daveslash
ID: 35084081
Putting a CTE in a view works beautifully for me:

create view delthis as  
  with ndm as (
    select *
      from member
     where deleteRequest = 'N'
  )
  select *
    from ndm

Open in new window

0
 
LVL 45

Expert Comment

by:Kdo
ID: 35084138
Wow!

I learned something again today.  I had no idea that the iSeries had moved that far into the present.  :)

Thanks, Dave!


Kent
0
 

Author Closing Comment

by:bobdylan75
ID: 35084230
THANK YOU EVERYBODY!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now