Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

SQL for creating dynamic table name

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: SQL for creating dynamic table name
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: 1753
Location: Bloomington, IL

PostPosted: Sat Jun 25, 2011 2:31 am    Post subject:
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    Post subject: i am not warred about the error
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: 1226
Location: Israel

PostPosted: Sat Jun 25, 2011 9:43 pm    Post subject:
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: 1753
Location: Bloomington, IL

PostPosted: Sun Jun 26, 2011 1:54 am    Post subject: Re: i am not warred about the error
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

Site Director


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

PostPosted: Sun Jun 26, 2011 2:06 am    Post subject:
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: 1226
Location: Israel

PostPosted: Sun Jun 26, 2011 5:08 pm    Post subject: Reply to: SQL for creating dynamic table name
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts how to send just 10 rows in a CICS sc... Megha Gupta CICS 5 Thu Feb 23, 2017 6:57 pm
No new posts Join of more than one table dibyendumandal11111 DB2 6 Mon Feb 13, 2017 11:36 pm
No new posts Dynamic array request/response contai... Suja.Sai CICS 2 Tue Jan 24, 2017 11:37 am
No new posts REXX DB2: Dynamic allocation of DB2.D... BHAS CLIST & REXX 3 Mon Dec 19, 2016 8:26 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us