[英]Oracle virtual column referencing another table
我有一個表TABLE1
列COL_TAB1
。
select * from TABLE1:
COL_TAB1
| 1 |
| 2 |
| 3 |
另一個表TABLE2
與COL_TAB2
並引用第一列COL_TAB1
select * from TABLE2:
COL_TAB2 | COL_TAB1
| val1 | 1
| val2 | 2
| val2 | 3
是否可以在 table1 上添加一個虛擬列,這樣我會得到與以下查詢相同的結果:
select
t1.COL_TAB1,
(select t2.COL_TAB2 from TABLE2 t2 where t2.COL_TAB1 = t1.COL_TAB1)
from TABLE1 t1
我試過了 :
alter table TABLE1 add (
SOME_COL_NAME as (select t2.COL_TAB2 from TABLE2 t2 where t2.COL_TAB1 = COL_TAB1)
)
但它給了我ORA-00936: missing expression
在這種情況下, 創建視圖可能是您最好的選擇。
CREATE VIEW vwTable1and2
AS
SELECT t1.COL_TAB1, t2.COL_TAB2
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON t1.COL_TAB1 = t2.COL_TAB1
那不是答案。 這是為了防止您測試這個不起作用的想法:
我試圖在一個函數內創建關節,並在虛擬列的定義中使用該函數。
CREATE TABLE A(
a1 integer,
a2 integer
);
CREATE TABLE B(
b1 integer,
b2 integer
);
CREATE FUNCTION f (arg IN integer)
RETURN INTEGER
IS
ret integer;
BEGIN
select B.b2 into ret from A join B on A.a1=B.b1
where A.a1=arg;
RETURN ret;
END;
alter TABLE A add (
a4 as (f(a1))
);
ORA-30553: 函數不確定
但它不起作用。 您需要確定性函數。 (它不包括在數據庫中使用查詢的函數)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.