Alter table add column as select 语句

Alter table add column as select statement

如何做这样的事情...

alter table customer_schedule add (week_number as (TO_CHAR((SCHEDULE_DATE),'iw')) 

其中SCHEDULE_DATE是table中已有的列之一

默认关键字应该让您在缺少某些值时存储值,但由于限制,you cannot use a column name

A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.

我认为行级 BEFORE INSERT/UPDATE 触发器应该做你想做的事情。

这是您需要虚拟列的地方。如果您使用的是 11g 及更高版本,您当然可以 -

alter table table_name
add (column_name [data_type] [generated always] as (column_expression) [virtual]);

在你的情况下,它将类似于 -

alter table customer_schedule add (week_number data_type generated always as (TO_CHAR((SCHEDULE_DATE),'iw') VIRTUAL) 

在 9i 上,您不能使用 虚拟列,所以我可能会使用视图:

create view customer_schedule_view as
    select 
      c.*,
      to_char(c.schedule_date, 'iw')) week_number
    from
      customer_schedule c;

当然,在您的表单中,您需要从视图 select 而不是 table。