sysdate和current_date有不同的类型?

Ali*_*ley 7 oracle

sysdate和current_date的oracle文档声称它们都返回DATE:

但是这个测试:

alter session set plsql_warnings = 'ENABLE:ALL';
create table test(x date);
create or replace procedure test1 authid definer is
    cursor s is select x from test where current_date > x;
begin for x in s loop null; end loop; end;
/
show errors
drop table test;
drop procedure test1;
Run Code Online (Sandbox Code Playgroud)

产生这个输出:

Errors for PROCEDURE TEST1:
LINE/COL  ERROR
3/42      PLW-07204: conversion away from column type may result in sub-optimal query plan
Run Code Online (Sandbox Code Playgroud)

使用sysdate不会给出相同的警告.我怀疑在查询中用current_date替换sysdate会冒更改查询计划的风险,尤其是在索引日期列的情况下.

编辑:

select dump(current_date) from dual;
select dump(sysdate) from dual;
Run Code Online (Sandbox Code Playgroud)

得到:

DUMP(CURRENT_DATE)
Typ=13 Len=8: 223,7,7,9,11,23,55,0

DUMP(SYSDATE)
Typ=13 Len=8: 223,7,7,9,11,23,55,0
Run Code Online (Sandbox Code Playgroud)

Nil*_*nde 1

1) CURRENT_DATE 返回会话时区的当前日期。你真的需要 current_date 吗?如果没有,请坚持使用 sysdate。这将使您的程序有效

2)如果您仍然需要CURRENT_DATE,以下是解决方案。将 current_date 的值存储到变量中,它将解决您的问题。如果这能回答您的问题,请告诉我。

drop table test;
create table test(x date);
create or replace procedure test1 authid definer 
is
dateVar date;
cursor s is select x from test where dateVar > x;
begin 
dateVar:=current_date;
for x in s loop null; 
end loop; 
end;
/
SQL> show errors
No errors.
Run Code Online (Sandbox Code Playgroud)