[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

DB2: V5R4M0: Create view and WITH statment

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
bobdylan75
Asked:
bobdylan75
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
bobdylan75Author Commented:
thank you but, it doesn't work in this way...
I tried 1 minute ago...
0
 
momi_sabagCommented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Dave FordSoftware Developer / Database AdministratorCommented:

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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Dave FordSoftware Developer / Database AdministratorCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
Wow!

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

Thanks, Dave!


Kent
0
 
bobdylan75Author Commented:
THANK YOU EVERYBODY!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now