我在執行存儲過程以取回結果時遇到問題。目前,我想在toad
,eclipse
的輸出窗口中顯示結果,或者我將要使用的任何其他結果。稍後,我想從程序中訪問它們。現在忽略程序部分,除非它是必需的。從存儲過程或函數獲取結果
我有這個存儲過程的代碼:
create or replace procedure pottyuserange (p_date_format in varchar2,
p_start_date in varchar2,
p_end_date in varchar2,
p_ref_cursor out sys_refcursor)
as
begin
open p_ref_cursor for
select to_char(time_range, p_date_format) as current_date,
lm_search,
ao_search,
ro_search,
fl_search,
total
from (select trunc(time_range) time_range,
sum(case when porta_potty = 'LM' then 1 else 0 end) as lm_search,
sum(case when porta_potty = 'AO' then 1 else 0 end) as ao_search,
sum(case when porta_potty = 'RO' then 1 else 0 end) as ro_search,
sum(case when porta_potty = 'FL' then 1 else 0 end) as fl_search,
sum(case when porta_potty in ('LM', 'AO', 'RO', 'FL') then 1 else 0 end) as total
from core.date_test
where trunc(time_range) >= to_date(p_start_date, p_date_format)
and trunc(time_range) <= to_date(p_end_date, p_date_format)
group by trunc(time_range))
order by time_range asc;
end pottyuserange;
/
而且我試圖得到這樣的結果:
variable rc refcursor;
BEGIN
pottyuserange('YYYY-MM-DD', '2008-10-1', '2010-10-12', :rc);
END;
print rc;
這是我得到的錯誤:
ORA-01722: invalid number
ORA-06512: at "core.pottyuserange", line 4
ORA-06512: at line 1
我該如何正確地訪問這些數據,或將它變成User-Defined Function
? \
這是我在把它變成一個功能的嘗試:
create or replace function pottyuserange (p_date_format in varchar2,
p_start_date in varchar2,
p_end_date in varchar2)
return result_set as rc sys_refcursor;
BEGIN
OPEN result_set FOR
select to_char(time_range, p_date_format) as current_date,
lm_search,
ao_search,
ro_search,
fl_search,
total
from (select trunc(time_range) time_range,
sum(case when porta_potty = 'LM' then 1 else 0 end) as lm_search,
sum(case when porta_potty = 'AO' then 1 else 0 end) as ao_search,
sum(case when porta_potty = 'RO' then 1 else 0 end) as ro_search,
sum(case when porta_potty = 'FL' then 1 else 0 end) as fl_search,
sum(case when porta_potty in ('LM', 'AO', 'RO', 'FL') then 1 else 0 end) as total
from core.date_test
where trunc(time_range) >= to_date(p_start_date, p_date_format)
and trunc(time_range) <= to_date(p_end_date, p_date_format)
group by trunc(time_range))
order by time_range asc;
return result_set;
end pottyuserange;
/
我與上面得到的錯誤是:PLS-00201: identifier 'RESULT_SET' must be declared
編輯:TIME_RANGE
是DATE
。 PORTA_POTTY
是一個varchar2。
,然後創建一個用戶定義的函數,而不是一個存儲過程。 –
我更新了問題。我將如何將其轉換爲用戶定義的函數?我一直在擺弄這個,但我沒有運氣。我會用我試過的東西來更新這篇文章。 –
涉及的數據類型是什麼?除非您的代碼正在進行某種隱式轉換,否則您得到的錯誤似乎沒有多大意義。但是這種轉換不會是明顯的。我猜''time_range'是'date' –