2016-03-18 52 views
1

是否有可能擁有一個虛擬列,其列表達式定義引用另一個虛擬列,該虛擬列之前已在表中定義過? The documentation for column expressions說:指向另一個虛擬列的虛擬列定義

the defining column_expression must refer only to columns of the subject table that have already been defined, in the current statement or in a prior statement

這是我想要做的,與第三虛擬列指的是其他虛擬列一個簡單的例子已經定義:

create table numberManipulation 
(
    value1 VARCHAR2 (10), 
    Value2 VARCHAR2 (10), 
    Addition AS (DECODE (value1, 'late', 1, 0) + DECODE (value2, 'late', 1, 0)), 
    subtraction AS(DECODE (value1, 'present', 1, 0) + DECODE (value2, 'present', 1, 0)), 
    Compile AS (ADDITION + SUBTRACTION) 
); 

但這會得到錯誤:

ORA-54012: virtual column is referenced in a column expression 

是我想要做的可能嗎?

回答

3

不,雖然the documentation for column expressions沒有明確說你不能。這一限制在My Oracle Support的文檔466916.1表示,雖然,在create table documentation

The column_expression in the AS clause has the following restrictions:

  • It cannot refer to another virtual column by name.
    ...

你將不得不從先前的每個虛擬列的重複計算在第三個:

create table numberManipulation 
(
    value1 VARCHAR2 (10), 
    Value2 VARCHAR2 (10), 
    Addition AS (DECODE (value1, 'late', 1, 0) + DECODE (value2, 'late', 1, 0)), 
    subtraction AS(DECODE (value1, 'present', 1, 0) + DECODE (value2, 'present', 1, 0)), 
    Compile AS (DECODE (value1, 'late', 1, 0) + DECODE (value2, 'late', 1, 0) 
    + DECODE (value1, 'present', 1, 0) + DECODE (value2, 'present', 1, 0)) 
); 

Table NUMBERMANIPULATION created. 

或者稍微更簡單:

Compile AS (DECODE (value1, 'late', 1, 'present', 1, 0) 
    + DECODE (value2, 'late', 1, 'present', 1, 0)) 

...儘管你可能寧願保持兩個較長表達式匹配的一致性和米早期專欄aintenance。

有一個虛擬列引用另一個虛擬列將決定他們必須被評估的順序,並可能導致循環依賴關係。

另一種方法是有計算基表從兩個虛擬列最後列的視圖:

create table numberManipulation 
(
    value1 VARCHAR2 (10), 
    Value2 VARCHAR2 (10), 
    Addition AS (DECODE (value1, 'late', 1, 0) + DECODE (value2, 'late', 1, 0)), 
    subtraction AS(DECODE (value1, 'present', 1, 0) + DECODE (value2, 'present', 1, 0)) 
); 

Table NUMBERMANIPULATION created. 

create view vNumberManipulation as 
select numberManipulation.*, addition + subtraction as compile 
from numberManipulation; 

View VNUMBERMANIPULATION created. 

insert into numberManipulation (value1, value2) values ('late', 'late'); 
insert into numberManipulation (value1, value2) values ('late', 'present'); 
insert into numberManipulation (value1, value2) values ('present', null); 
select * from vNumberManipulation; 

VALUE1  VALUE2  ADDITION SUBTRACTION  COMPILE 
---------- ---------- ----------- ------------ ------------ 
late  late     2   0   2 
late  present    1   1   2 
present       0   1   1 

的視圖也不能引用自己的列,所以你還需要增加和在基表中減法。您也可以在視圖上使用instead-of trigger,以便嘗試修改視圖實際上會更改基表,這會使其更友好。

+0

謝謝,但問題是,我給這裏就像樣本,通常有value1 ..... value28,有沒有其他方法可以做到這一點,因爲它會更復雜,鍵入太多 – Programmer

+0

你可以有一個該視圖基於基表的虛擬列計算列值;如果需要的話可以使用替代觸發器。或者你可以通過觸發器設置一個物理列,但這也不是理想的。 –

+0

o ..是否有任何參考或關鍵點可以基於它進行搜索。 – Programmer