5

I am trying to change a data type of a column in a Oracle View by executing the following statement:

ALTER VIEW <view_name> 
MODIFY (ID VARCHAR2(100));

I get the following error:

Error starting at line : 1 in command -
ALTER VIEW <view_name>
MODIFY (ID VARCHAR2(100))
Error report -
ORA-00922: missing or invalid option
00922. 00000 -  "missing or invalid option"
*Cause:    
*Action:

I referred to the post here. What's the correct way to achieve this? I guess Oracle expects CONSTRAINT keyword after MODIFY. The column I am modifying the data type of is one of the primary keys in the table on which this view stands.

Nikhil
  • 621
  • 1
  • 13
  • 25
  • 3
    You can't change the reported data type of the view - as has been stated in the linked question. The only way to achieve that, is to use an appropriate CAST in the view's source. –  Aug 20 '19 at 14:22

2 Answers2

11

You can not change the column data type of the VIEW.

Instead, you can change the data type of underlying base table's column (In that case refer the answer of the link mentioned in your question) or you can create a view query with that data type.

-- Your view
CREATE OR REPLACE VIEW <YOUR VIEW> AS
SELECT ID, 
...
FROM <TABLE>;

You can change the SELECT query of the view.

CREATE OR REPLACE VIEW <YOUR VIEW> AS
SELECT cast(ID as varchar2(20)) as ID, 
...
FROM <TABLE>;

Hope, this will help.

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31
7

You cannot change the column size of a view using ALTER. You just can't.

If you want to change a view definition, you have to find the original definition and rewrite it.

To find the original definition, look for the TEXT or TEXT_VC column in USER_VIEWS. Then use CAST to adjust the definition of the column of interest.

Example:

SQL> create or replace view V as
  2  select dummy from dual;

View V created.

SQL> desc v;

Name    Null?   Type          
DUMMY            VARCHAR2(1)    

SQL> select text_vc from user_views where view_name = 'V';

TEXT_VC       
----------------------
select dummy from dual    


SQL> create or replace view V as 
  2  select cast(dummy as varchar2(100)) dummy from dual;

View V created.

SQL> desc v;

Name    Null?   Type            
DUMMY           VARCHAR2(100) 

Best regards,

Stew Ashton

Stew Ashton
  • 1,499
  • 9
  • 6