Solved

DB2: V5R4M0: Create view and WITH statment

Posted on 2011-03-09
8
783 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 18

Expert Comment

by:Dave Ford
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
 
LVL 45

Expert Comment

by:Kent Olsen
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:Dave Ford
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:Kent Olsen
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

726 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