View previous topic :: View next topic |
Author |
Message |
yadav2005 Intermediate
Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Wed May 18, 2011 8:07 am Post subject: Unable to create view without a column in base table |
|
|
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 |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12368 Topics: 75 Location: San Jose
|
Posted: Wed May 18, 2011 10:13 am Post subject: |
|
|
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 |
|
|
yadav2005 Intermediate
Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Wed May 18, 2011 11:57 am Post subject: |
|
|
Kolusu,
Can you please help me fix the problem. My view creation is not successful. |
|
Back to top |
|
|
Dibakar Advanced
Joined: 02 Dec 2002 Posts: 699 Topics: 63 Location: USA
|
Posted: Wed May 18, 2011 12:08 pm Post subject: |
|
|
Do you know why you are getting -158? _________________ Regards,
Diba |
|
Back to top |
|
|
yadav2005 Intermediate
Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Wed May 18, 2011 12:11 pm Post subject: |
|
|
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 |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12368 Topics: 75 Location: San Jose
|
Posted: Wed May 18, 2011 12:12 pm Post subject: |
|
|
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 |
|
|
yadav2005 Intermediate
Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Wed May 18, 2011 12:35 pm Post subject: |
|
|
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 |
|
|
Dibakar Advanced
Joined: 02 Dec 2002 Posts: 699 Topics: 63 Location: USA
|
Posted: Wed May 18, 2011 12:41 pm Post subject: |
|
|
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 |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12368 Topics: 75 Location: San Jose
|
Posted: Wed May 18, 2011 12:49 pm Post subject: |
|
|
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 |
|
|
|
|