How to change a column data type in Oracle View?

Nik*_*hil 1 sql oracle sql-view

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));
Run Code Online (Sandbox Code Playgroud)

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:
Run Code Online (Sandbox Code Playgroud)

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.

Tej*_*ash 6

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>;
Run Code Online (Sandbox Code Playgroud)

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>;
Run Code Online (Sandbox Code Playgroud)

Hope, this will help.

Cheers!!


小智 5

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) 
Run Code Online (Sandbox Code Playgroud)

Best regards,

Stew Ashton