Oracle9i安裝默認(rèn)的日期格式是‘DD-MM-RR’,這個(gè)可以通過
select * from sys.nls_database_parameters;
進(jìn)行查看。
因此對于輸入?yún)?shù)為DATE類型的存儲過程就出現(xiàn)問題了,其中查詢基本表(tranficstat
)里記錄日期格式為‘yyyy-mm-dd’。原碼如下:
--比較某兩個(gè)車站相同時(shí)間段的運(yùn)力情況
create or replace PRocedure HY_CONTRAST_PERIOD(
depotcode1 in varchar2,
depotcode2 in varchar2,
startdate1 in date,
enddate1 in date,
cur_return out CUR_DEFINE.GB_CUR) is --CUR_DEFINE.GB_CUR 是自定義的游標(biāo)類型
begin
case
when (depotcode1 is null) or (depotcode2) is null then
return;
else
open cur_return for
select
sum(NORMAL_SCHEMES) as 正班班次,
sum(OVERTIME_SCHEMES) as 加班班次,
sum(NORMAL_SEATS) as 正班座位數(shù),
sum(OVERTIME_SEATS) as 加班座位數(shù)
from tranficstat
where senddate >= startdate1
and senddate < enddate1+ 1
and depot = depotcode1
group by depot
union
select
sum(NORMAL_SCHEMES) as 正班班次,
sum(OVERTIME_SCHEMES) as 加班班次,
sum(NORMAL_SEATS) as 正班座位數(shù),
sum(OVERTIME_SEATS) as 加班座位數(shù)
from tranficstat
where senddate >= startdate1
and senddate < enddate1 + 1
and depot = depotcode2
group by depot;
end case;
end HY_CONTRAST_PERIOD;
通過union,你期望返回兩條記錄,卻發(fā)現(xiàn)永遠(yuǎn)總是只返回一條記錄。問題癥結(jié)發(fā)生在日期格式轉(zhuǎn)換上,參數(shù)傳進(jìn)的格式為‘dd-mm-rr’,而條件左側(cè)的記載格式為‘yyyy-mm-dd’,只要把所有右側(cè)條件更改成如
where senddate >= to_date(to_char(startdate1,'yyyy-mm-dd'),'yyyy-mm-dd')
and senddate < to_date(to_char(enddate1,'yyyy-mm-dd'),'yyyy-mm-dd') + 1;
即可消除癥狀。
當(dāng)然也可以修改左側(cè)的格式,總之使兩邊的日期格式匹配;另外當(dāng)然也可以直接修改系統(tǒng)的NLS_DATE_FORMAT 。