MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unable to create view without a column in base table

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Wed May 18, 2011 8:07 am    Post subject: Unable to create view without a column in base table Reply with quote

Members,

I need to create a View with 5 columns . Basically I do not have column EMPGENDER in the base table but still I just want to create a view so that EMPGENDER column has DEFAULT NOT NULL WITH VALUE SPACE.When I try to create the View I get -158 SQL code. Somehow I need to have a column EMPGENDER with space.
Code:

CREATE VIEW V1
(
EMPNO,
EMPNAME
EMPGENDER,
EMPAGE,
EMPSALARY
)
AS
SELECT
EMPNO,
EMPNAME
EMPAGE,
EMPSALARY
FROM
EMPLOYEE;
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Wed May 18, 2011 10:13 am    Post subject: Reply with quote

yadav2005,

View doesn't contain any data. It is just pulling information from one or more base tables on which it is defined.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Wed May 18, 2011 11:57 am    Post subject: Reply with quote

Kolusu,

Can you please help me fix the problem. My view creation is not successful.
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Wed May 18, 2011 12:08 pm    Post subject: Reply with quote

Do you know why you are getting -158?
_________________
Regards,
Diba
Back to top
View user's profile Send private message Send e-mail
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Wed May 18, 2011 12:11 pm    Post subject: Reply with quote

Yes because the columns which I am having in my View creation do not match in count in the Select from columns from base table.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Wed May 18, 2011 12:12 pm    Post subject: Reply with quote

yadav2005 wrote:
Kolusu,

Can you please help me fix the problem. My view creation is not successful.


You need to go back and read the basics of a VIEW as there is nothing I can do to fix the problem.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Wed May 18, 2011 12:35 pm    Post subject: Reply with quote

Kolusu,

I might be lacking basics here. But -158 incidates that the column which I want an additional in the View creation is not present in the base table , as the column numbers does not match. I am trying to understand the explanation better but not able to proceed further. I am stuck here.
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Wed May 18, 2011 12:41 pm    Post subject: Reply with quote

Try this to add EMPGENDER in the SELECT -

Code:
CREATE VIEW V1
(
 EMPNO,
 EMPNAME
 EMPGENDER,
 EMPAGE,
 EMPSALARY
)
AS
SELECT
 EMPNO,
 EMPNAME,
 CAST (NULL AS CHAR(1)) AS EMPGENDER,
 EMPAGE,
 EMPSALARY
FROM
EMPLOYEE;


edit: forgot comma after EMPNAME in SELECT
_________________
Regards,
Diba


Last edited by Dibakar on Wed May 18, 2011 2:21 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Wed May 18, 2011 12:49 pm    Post subject: Reply with quote

yadav2005,

In simple terms a view can related to a a screen to view the data from the base table. The view does NOT exist physically. Any changes to the data are done on the base table/tables the view is defined. So you just can't create a view with a column that does NOT exist in the base table. Think logically for a second as to where would you store the gender value? when the view physically doesn't exist?

The SQLCODE of -158 is notifies you about the mismatch in the columns defined vs number of columns in the select clause. DB2 does not have the ability to pull the data from thin air into the 5th column when you are only selecting 4 columns in your select statement.

on second thoughts why do you even need a View for this? Isn't a simple SELECT like this satisfy your requirement?

Code:

SELECT EMPNO
      ,EMPNAME
      ,EMPAGE
      ,EMPSALARY
      ,CHAR(' ')
  FROM EMPLOYEE;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group