自动编译列的一些奇特示例
-- create base tables
CREATE TABLE investor
(investor_id VARCHAR2(30),
total_amt_invested INTEGER,
current_value INTEGER);
CREATE TABLE investment_info
(investor_id VARCHAR2(30),
entity_id VARCHAR2(30),
no_of_units_bought INTEGER);
CREATE TABLE entity_info
(entity_id VARCHAR2(30),
listed_npv NUMBER,
current_npv INTEGER);
-- insert data
insert into investor values ('ABC', 100, null);
insert into investment_info values ('ABC', 'XYZ1', 400);
insert into investment_info values ('ABC', 'XYZ2', 600);
insert into investment_info values ('ABC', 'XYZ3', 1000);
insert into entity_info values ('XYZ1', .05, 1);
insert into entity_info values ('XYZ2', .05, 2);
insert into entity_info values ('XYZ3', .05, 3);
insert into investor values ('BCD', 100, null);
insert into investment_info values ('BCD', 'XYZ4', 350);
insert into entity_info values ('XYZ4', .05, 4);
commit;
-- create function to return total invest amount
CREATE OR REPLACE FUNCTION get_total_invest(p_investor_id VARCHAR2)
RETURN NUMBER
DETERMINISTIC
RESULT_CACHE
-- you don't need RELIES_ON if version 11.2+
RELIES_ON (investment_info, entity_info)
IS
l_result NUMBER;
BEGIN
SELECT sum(no_of_units_bought * current_npv)
INTO l_result
FROM investment_info
JOIN entity_info
ON investment_info.entity_id = entity_info.entity_id
WHERE investor_id = p_investor_id;
RETURN l_result;
END;
/
-- alter table to add autocalculated virtual column
ALTER TABLE investor ADD (auto_current_value GENERATED ALWAYS AS (get_total_invest(investor_id)));
-- recreate function without DETERMINISTIC (read documantion about virtual column)
CREATE OR REPLACE FUNCTION get_total_invest(p_investor_id VARCHAR2)
RETURN NUMBER
RESULT_CACHE
-- you don't need RELIES_ON if version 11.2+
RELIES_ON (investment_info, entity_info)
IS
l_result NUMBER;
BEGIN
SELECT sum(no_of_units_bought * current_npv)
INTO l_result
FROM investment_info
JOIN entity_info
ON investment_info.entity_id = entity_info.entity_id
WHERE investor_id = p_investor_id;
RETURN l_result;
END;
/
SELECT *
FROM investor;
insert into investment_info values ('ABC', 'XYZ3', 1100);
COMMIT;
SELECT *
FROM investor;