create or replace function getAvg(id1 IN number, id2 IN number) return number as
sal1 number;
sal2 number;
avg number;
BEGIN
select esal into sal1 from employees where eno = id1;
select esal into sal2 from employees where eno = id2;
avg := (sal1 sal2)/2;
return avg;
END;
/
當我嘗試編譯上述代碼時,我收到以下訊息的編譯錯誤:
Warning: Function created with compilation errors.
但是當我用它替換avg之后編譯成功。return(sal1 sal2)/2
uj5u.com熱心網友回復:
這是壞習慣:永遠不要使用保留字或關鍵字來命名您自己的物件、變數。avg是一個內置函式;重命名變數:
SQL> create or replace function getAvg(id1 IN number, id2 IN number)
2 return number
3 as
4 sal1 number;
5 sal2 number;
6 l_avg number;
7 BEGIN
8 select esal into sal1 from employees where eno = id1;
9 select esal into sal2 from employees where eno = id2;
10 l_avg := (sal1 sal2)/2;
11 return l_avg;
12 END;
13 /
Function created.
SQL> select * from employees;
ENO ESAL
---------- ----------
1 100
2 200
SQL> select getavg(1, 2) from dual;
GETAVG(1,2)
-----------
150
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/407674.html
標籤:
上一篇:計算遞回“n”數
下一篇:來自表中不存在的列的位置
