第一步: 创建函数, 目的是创建虚拟所需的DETERMINISTIC函数
CREATE OR REPLACE FUNCTION GetTaskProgress (a_task_no task.task_no%TYPE)
RETURN PLS_INTEGER
DETERMINISTIC
RESULT_CACHE
IS
v_Task_Progress task.progress%TYPE;
BEGIN
v_Task_Progress := 10;
RETURN v_Task_Progress;
EXCEPTION
WHEN OTHERS
THEN
-- Consider logging the error and then re-raise
RAISE;
END GetTaskProgress;
/
第二步: 添加虚拟列:
alter table task add ( calcprogress as (GetTaskProgress(task_no)));
第三步: 修改原来的列名
ALTER TABLE TASK
RENAME COLUMN PROGRESS TO ORGINPROGRESS ;
第四步: 使用正确的函数
CREATE OR REPLACE FUNCTION GetTaskProgress (a_task_no task.task_no%TYPE)
RETURN PLS_INTEGER
RESULT_CACHE
IS
/******************************************************************************
NAME: GetTaskProgress
PURPOSE:
计算任务的进展
REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 2015-10-8 Administrator 1. Created this function.
NOTES:
******************************************************************************/
v_Task_Progress PLS_INTEGER;
BEGIN
BEGIN
SELECT NVL (ORGINPROGRESS, 0)
INTO v_Task_Progress
FROM TASK A
WHERE NOT EXISTS
(SELECT NULL
FROM task B
WHERE B.parent_task_no = A.task_no)
AND task_no = a_task_no; --- 叶子任务
EXCEPTION
WHEN NO_DATA_FOUND
THEN -- 非叶子任务
SELECT SUM (NVL (WEIGHT, 0) * GetTaskProgress (task_no) / 100)
INTO v_Task_Progress
FROM task
WHERE parent_task_no = a_task_no
GROUP BY parent_task_no;
END;
RETURN v_Task_Progress;
EXCEPTION
WHEN OTHERS
THEN
-- Consider logging the error and then re-raise
RAISE;
END GetTaskProgress;
/
第五步: 修改计算列名字
ALTER TABLE TASK
RENAME COLUMN CALCPROGRESS TO PROGRESS ;