在 PL SQL 中比较日期
compare date on PLSQL
如果我尝试从 table 中删除并且 table 上的日期等于 sysdate
。
,我必须创建引发错误的触发器
这是我的代码:
create or replace trigger nonViolation
before insert or delete on reservation
for each row
declare
error exception;
error2 exception;
dateres date:=sysdate;
begin
if inserting then
if :new.dateDep<dateres then
raise error;
end if;
end if;
if deleting then
if (:OLD.datedep=dateres)
then
raise error2;
end if;
end if;
exception
when error2 then
raise_application_error(-20003,'supression impossible');
when error then
raise_application_error(-20001,'reservation impossible');
end;
我试图删除包含今天date 25-nov-18
的行,但触发器不起作用。我尝试打印 :old.datedep
和 dateres
的值,它们是相同的:
25-nov-2018
25-nov-2018
有人可以帮助我吗?谢谢
我怀疑您没有考虑到 SYSDATE 包含当前时间和当前日期这一事实。如果像这样删除时间元素,您的触发器可能会产生所需的结果:
...
error2 exception;
dateres date := trunc(sysdate);
begin
...
"it doesn't work ,"
正如@bobjarvis 建议您可能需要从 datedep
中删除时间元素(取决于您如何填充它):
if TRUNC(:OLD.datedep) = dateres
尝试使用完整的日期格式掩码,例如 'yyyy-mm-dd hh24:mi:ss'
,看看您实际拥有什么。
在 Oracle 上,DATE 类型的列和变量具有日期部分(年-月-日)和时间部分(24 小时制中的小时、分钟、秒)。
您 "see" 在屏幕上的显示方式取决于开发工具和工具的本地配置。
一些工具如果时间分量是午夜 (00:00:00) 则不显示时间分量。
此外,您的 country/session 区域会影响工具显示日期的方式。
不带第二个参数的 DATE 类型值的 TRUNC 函数给出一个 DATE 类型的值,其组成时间为午夜 (00:00:00)。
所以要比较同一天使用的 2 个值:
Trunc(:old.datedep)=trunc(dateres)
如果我尝试从 table 中删除并且 table 上的日期等于 sysdate
。
这是我的代码:
create or replace trigger nonViolation
before insert or delete on reservation
for each row
declare
error exception;
error2 exception;
dateres date:=sysdate;
begin
if inserting then
if :new.dateDep<dateres then
raise error;
end if;
end if;
if deleting then
if (:OLD.datedep=dateres)
then
raise error2;
end if;
end if;
exception
when error2 then
raise_application_error(-20003,'supression impossible');
when error then
raise_application_error(-20001,'reservation impossible');
end;
我试图删除包含今天date 25-nov-18
的行,但触发器不起作用。我尝试打印 :old.datedep
和 dateres
的值,它们是相同的:
25-nov-2018
25-nov-2018
有人可以帮助我吗?谢谢
我怀疑您没有考虑到 SYSDATE 包含当前时间和当前日期这一事实。如果像这样删除时间元素,您的触发器可能会产生所需的结果:
...
error2 exception;
dateres date := trunc(sysdate);
begin
...
"it doesn't work ,"
正如@bobjarvis 建议您可能需要从 datedep
中删除时间元素(取决于您如何填充它):
if TRUNC(:OLD.datedep) = dateres
尝试使用完整的日期格式掩码,例如 'yyyy-mm-dd hh24:mi:ss'
,看看您实际拥有什么。
在 Oracle 上,DATE 类型的列和变量具有日期部分(年-月-日)和时间部分(24 小时制中的小时、分钟、秒)。
您 "see" 在屏幕上的显示方式取决于开发工具和工具的本地配置。
一些工具如果时间分量是午夜 (00:00:00) 则不显示时间分量。
此外,您的 country/session 区域会影响工具显示日期的方式。
不带第二个参数的 DATE 类型值的 TRUNC 函数给出一个 DATE 类型的值,其组成时间为午夜 (00:00:00)。
所以要比较同一天使用的 2 个值:
Trunc(:old.datedep)=trunc(dateres)