gpt4 book ai didi

oracle - sysdate 和 current_date 有不同的类型?

转载 作者:行者123 更新时间:2023-12-04 12:53:18 29 4
gpt4 key购买 nike

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

  • sysdate
  • current_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;

    产生这个输出:
    Errors for PROCEDURE TEST1:
    LINE/COL ERROR
    3/42 PLW-07204: conversion away from column type may result in sub-optimal query plan

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

    编辑:
    select dump(current_date) from dual;
    select dump(sysdate) from dual;

    给出:
    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

    最佳答案

    1) CURRENT_DATE 返回 session 时区中的当前日期。你真的需要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.

    关于oracle - sysdate 和 current_date 有不同的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303776/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com