2016-01-22 31 views
1

我有麻煩編譯如下功能的Oracle甲骨文 - PLS-00382:表達式是錯誤的類型在減去日期

CREATE OR REPLACE FUNCTION fn1 
return binary_double 
as 
Fecha DATE; 
Dias binary_double; 
begin 
dbms_output.put_line(CAST(TRUNC(CURRENT_DATE,'DD') AS DATE) - cast(current_timestamp as date)); 
return Dias; 
end; 

它導致錯誤

Error(8,27): PLS-00382: expression is of wrong type 

但是外跑同樣的表情函數體

select CAST(TRUNC(CURRENT_DATE,'DD') AS DATE) - cast(current_timestamp as date) from dual 

給出預期結果 - 兩個日期之間的天數

-0.0607060185185185185185185185185185185185 

有什麼想法可能是錯誤的功能?

回答

2

用途:

begin 
    dbms_output.put_line(TRUNC(CURRENT_DATE,'DD') - cast(current_timestamp as date)); 
end; 
/

CURRENT_DATE返回數據類型DATE值
Trunct也返回日期。
所以你試圖將DATE類型轉換爲DATE類型。

2

似乎PL/SQL有一個類型轉換爲相同類型的問題。

CURRENT_DATE返回DATE數據類型,因此您將其轉換爲相同類型。它在SQL中工作,但不在PL/SQL中。只需刪除鑄件:

CREATE OR REPLACE FUNCTION fn1 
return binary_double 
as 
Fecha DATE; 
Dias binary_double; 
begin 
dbms_output.put_line(TRUNC(CURRENT_DATE,'DD') - cast(current_timestamp as date)); 
return Dias; 
end;