2012-03-22 63 views
1

嘿我試了很多時間,但我無法修復這個簡單的程序,爲我的個人網站工作。我想提取當前時間並顯示一條消息用它。在Oracle中的功能,以獲得時間與顯示消息:)

喜歡當前時間是:上午12:00

這是我的代碼。

Create Function A1SF_GETTIME Is 

    Temptime Char(20); 
    Message Varchar2(20) := 'The Current Time is :'; 
    Begin 
    Select to_char(sysdate, 'HH24:MI:SS') As "Current Time" 
    Into TempTime 
    From dual; 
    dbms_output.put_line(Message || ' ' || TempTime);  
End; 

回答

3

函數需要在函數定義一個RETURN條款。它必須有一個RETURN語句,以便該函數實際返回某些內容。如果解決這個問題,用事實,其他人已經指出,局部變量Message太小了一起,你可以做這樣的事情

SQL> ed 
Wrote file afiedt.buf 

    1 Create or replace Function A1SF_GETTIME 
    2 return varchar2 
    3 Is 
    4 l_time varchar2(10) := to_char(sysdate, 'HH24:MI:SS'); 
    5 l_message Varchar2(30) := 'The Current Time is : ' || l_time; 
    6 Begin 
    7 dbms_output.put_line(l_message); 
    8 return l_message; 
    9* End; 
SQL>/

Function created. 

SQL> select a1sf_gettime from dual; 

A1SF_GETTIME 
-------------------------------------------------------------------------------- 
The Current Time is : 14:09:47 
+0

喜歡你的答案,因爲它也返回,這將是在非常有用的消息我從Asp.net或Vb開始 – 2012-03-23 11:09:17

2

這個怎麼樣?

to_char(sysdate, '"The current time is "hh24:mi:ss') 

SQL> select to_char(sysdate, '"The current time is "hh24:mi:ss') from dual; 

TO_CHAR(SYSDATE,'"THECURRENT 
---------------------------- 
The current time is 09:16:08 
1

不知道是什麼錯誤,因爲你還沒有表現出它:-),但你可以大大的東西,如簡化代碼:

create function A1SF_GETTIME is 
    Temptime Char(50); 
begin 
    select 'The current time is ' || to_char (sysdate, 'HH24:MI:SS') 
     into TempTime from dual; 
    dbms_output.put_line (TempTime); 
end; 

您必須增加緩衝區大小,因爲它保存所有數據,並且50應該足夠大,以便生成字符串。這可以通過完全擺脫Message的事實來抵消。

2

你的代碼並不是那麼糟糕......你的字符串緩衝區對於你的消息太小了!

這會工作:

Message VARCHAR2(21)