IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SQL for creating dynamic table name


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sree reddy

New User


Joined: 20 Jul 2007
Posts: 27
Location: bangalore

PostPosted: Sat Jun 25, 2011 1:57 am
Reply with quote

Code:
SELECT 'SELECT * FROM FEPG2.'                                 
       ||'R'                                                   
       ||SUBSTR(RTRIM(CHAR(YEAR(CURRENT DATE))),3,2)           
       ||'_CHG_AUX_FACT'                                       
       ||MONTH1                                               
FROM                                                           
   (SELECT CASE WHEN MONTH(CURRENT DATE) < 10                 
                     THEN '0'||RTRIM(CHAR(MONTH(CURRENT DATE)))
                ELSE                                           
                     RTRIM(CHAR(MONTH(CURRENT DATE)))         
           END MONTH1                                         
    FROM SYSIBM.SYSDUMMY1                                     
   ) A       

The above sQL creates SELECT * FROM FEPG2.R11_CHG_AUX_FACT06

My Question/request
I am using the above sql for creating dynamic table name because the Year and month changes e the table name changes every onth and the view name remains same

i want to create view from the above sql

i tried create view viewname as (the above sql)
i am getting error

Code:
DSNT408I SQLCODE = -153, ERROR:  THE STATEMENT IS INVALID BECAUSE THE VIEW OR   
         TABLE DEFINITION DOES NOT INCLUDE A UNIQUE NAME FOR EACH COLUMN       
DSNT418I SQLSTATE   = 42908 SQLSTATE RETURN CODE                               
DSNT415I SQLERRP    = DSNXOVD SQL PROCEDURE DETECTING ERROR                     
DSNT416I SQLERRD    = -130 0  0  -1  0  0 SQL DIAGNOSTIC INFORMATION           
DSNT416I SQLERRD    = X'FFFFFF7E'  X'00000000'  X'00000000'  X'FFFFFFFF'       
         X'00000000'  X'00000000' SQL DIAGNOSTIC INFORMATION                   
---------+---------+---------+---------+---------+---------+---------+---------+

Please suggest me how to make this work


I want to create View
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Sat Jun 25, 2011 2:31 am
Reply with quote

Is it not the case that the SQLCODE indicates that there is a problem with the view definition, not with the query?
Back to top
View user's profile Send private message
sree reddy

New User


Joined: 20 Jul 2007
Posts: 27
Location: bangalore

PostPosted: Sat Jun 25, 2011 9:14 pm
Reply with quote

i am not warred about the error i need solution how to create a view based on the above SQL

Please help me
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sat Jun 25, 2011 9:43 pm
Reply with quote

My guess:
You receive this error because you're creating the view on the original SELECT, not on the resulting row.

Please show how you build the CREATE VIEW statement.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Sun Jun 26, 2011 1:54 am
Reply with quote

sree reddy wrote:
i am not warred [sic] about the error

Oh? Although I don't have access to a mainframe today, the message
Code:
THE STATEMENT IS INVALID BECAUSE THE VIEW OR   
TABLE DEFINITION DOES NOT INCLUDE A UNIQUE NAME FOR EACH COLUMN   

suggests that your problem is pretty obvious. Perhaps GuyC or another SQL maven could resolve this impasse for us.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sun Jun 26, 2011 2:06 am
Reply with quote

Hello,

Quote:
i am not warred [sic] about the error


Quote:
Please help me


Until you do become "worried" about the errors actually presented to you by the system, i don't know how we can help. Just humour us and explain why there need be no concern for this error.

Explain what the system is telling you that you should be able t ignore.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sun Jun 26, 2011 5:08 pm
Reply with quote

Today's your lucky day...

Code:
//*---------------------------------------------------
//* CREATE 'CREATE VIEW' ROW                         
//*---------------------------------------------------
//STEP1    EXEC PGM=SYNCSORT,PARM='DB2=DSN'
//SYSOUT   DD   SYSOUT=*
//SORTDBIN DD   *
  SELECT CAST (
         'CREATE VIEW XXYYZZ AS (SELECT * FROM FEPG2.'
         ||'R'                                       
         ||SUBSTR(CHAR(CURRENT DATE),9,2)             
         ||'_CHG_AUX_FACT'                           
         ||SUBSTR(CHAR(CURRENT DATE),4,2)    || ')'   
          AS CHAR(80)  )                             
      FROM SYSIBM.SYSDUMMY1                           
//SORTOUT  DD   DISP=(NEW,PASS),DSN=&TEMPCR
//SYSIN    DD   *
  SORT FIELDS=COPY
/*
//*---------------------------------------------------
//* RUN 'CREATE VIEW'                                 
//*---------------------------------------------------
//STEP2    EXEC DSNTEP2,SYSTEM=DSN
//SYSIN    DD   DISP=(OLD,DELETE),DSN=&TEMPCR         


Note 1: I used SYNCSORT, you can use whatever tool you have to generate the create view row.
Note 2: I use a proc to run DSNTEP2, use your own convention.
Note 3: for me, current date is in european format (DD.MM.YYYY).
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Using Dynamic file handler in the Fil... COBOL Programming 2
No new posts Check data with Exception Table DB2 0
Search our Forums:

Back to Top