日期:2014-05-17  浏览次数:20974 次

如何把表里某类修改为计算列
比如:create table tb(t1 int,t2 int,t3 int , t4 int)

我想把t4改成计算列 t4=t1+t2+t3,怎么修改这列,修改语句怎么写




------解决方案--------------------
update tb set t4=t1+t2+t3
------解决方案--------------------
update tb set t4=nvl(t1,0)+nvl(t2,0)+nvl(t3,0);
------解决方案--------------------
如果你需要的是建立一个虚拟列,那么只能先将这一列删除,然后再创建一个新的虚拟列
SQL code


SQL>  create table tb(t1 int,t2 int,t3 int , t4 int) ;

Table created.


SQL>  create table tb(t1 int,t2 int,t3 int , t4 int) ;

Table created.

SQL> commit ;

Commit complete.

SQL> alter table tb modify t4 as (t1+t2+t3) ;
alter table tb modify t4 as (t1+t2+t3)
                      *
ERROR at line 1:
ORA-54026: Real column cannot have an expression

SQL> alter table tb drop column t4 ;

Table altered.

[color=#FF0000]SQL> alter table tb add t4 as (t1+t2+t3) ;
[/color]
Table altered.

SQL> select * from tb ;

        T1         T2         T3         T4
---------- ---------- ---------- ----------
         1          1          1          3
         2          2          2          6
         3          3          3          9
         4          4          4         12
         5          5          5         15
         6          6          6         18
         7          7          7         21
         8          8          8         24
         9          9          9         27
        10         10         10         30

10 rows selected.