尝试在 oracle 中触发时出错

发布于 2024-08-19 10:24:22 字数 1106 浏览 4 评论 0原文

我正在尝试使用 ORACLE SQL DEVELOPER 在 ORACLE 中创建触发器,我会检查作为开始日期预订输入的日期到预订日期或服务费日期是否等于或大于记录中插入的当前日期。

这些是我的表中的字段,

Service (date_service, cost_variation, number_room, id_service);

这是我的代码:

CREATE OR REPLACE TRIGGER VERIFY_DATE
BEFORE INSERT OR UPDATE OF FECHA_PLAN ON SERVICE
FOR EACH ROW
DECLARE
  fecha_ac DATE;
BEGIN
  SELECT SYSDATE INTO fecha_ac FROM DUAL;
  IF(:NEW.FECHA_PLAN > fecha_ac)THEN
    dbms_output.put_line('The date of the plan should be more than the current date ');
    raise_application_error(-20601, 'Dato invalido');
  END IF;
END;

这是我尝试运行 TRIGGER 时遇到的错误将

INSERT INTO "MIGRARBD"."SERVICE" 
    (date_service, cost_variation, number_room, id_service) 
VALUES 
    (TO_DATE('20/01/10', 'DD/MM/RR'), '2', '1', '1')

更改保存到表“MIGRARBD”时出现错误。“SERVICE”:

Fila 1: ORA-20601: Dato invalido
ORA-06512: en "MIGRARBD.VERIFICAR_FECHA", línea 7
ORA-04088: error during execution of trigger 'MIGRARBD.VERIFICAR_FECHA'
ORA-06512: on line 1

我希望您能提供帮助...请原谅我的英语

i'm trying to make a TRIGGER in ORACLE using ORACLE SQL DEVELOPER, I would check that the dates entered as start date reservation to reservation date or date of a service charge will be equal to or greater than the current date is inserted in the record.

these are the fields in my table

Service (date_service, cost_variation, number_room, id_service);

this is my code:

CREATE OR REPLACE TRIGGER VERIFY_DATE
BEFORE INSERT OR UPDATE OF FECHA_PLAN ON SERVICE
FOR EACH ROW
DECLARE
  fecha_ac DATE;
BEGIN
  SELECT SYSDATE INTO fecha_ac FROM DUAL;
  IF(:NEW.FECHA_PLAN > fecha_ac)THEN
    dbms_output.put_line('The date of the plan should be more than the current date ');
    raise_application_error(-20601, 'Dato invalido');
  END IF;
END;

and this is the error I get when trying to run the TRIGGER

INSERT INTO "MIGRARBD"."SERVICE" 
    (date_service, cost_variation, number_room, id_service) 
VALUES 
    (TO_DATE('20/01/10', 'DD/MM/RR'), '2', '1', '1')

There was an error when saving changes to table "MIGRARBD"."SERVICE":

Fila 1: ORA-20601: Dato invalido
ORA-06512: en "MIGRARBD.VERIFICAR_FECHA", línea 7
ORA-04088: error during execution of trigger 'MIGRARBD.VERIFICAR_FECHA'
ORA-06512: on line 1

I hope you can help ... and excuse my English

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-08-26 10:24:22

您的触发器引用了 FECHA_PLAN,它与您定义表的方式不匹配。据推测,它与 DATE_SERVICE 相同。

正如您所编码的那样,如果输入的日期大于当前日期,则触发器将失败。但当你说...

我会检查输入的日期
...将等于或大于
当前日期被插入到
记录一下。

...也许您想要的是强制执行一条规则,即日期必须大于或等于当前日期。如果是这样,如果输入的日期小于当前日期,您的触发器应该失败。像这样...

IF(:NEW.FECHA_PLAN < sysdate )THEN

请注意,我们可以直接使用sysdate,因此select ... from Dual是不必要的(除非正如 Rene 在评论中指出的那样,您希望多次使用相同的值)。

如果这不能解决您的问题,您将需要进行更多解释。

Your trigger references FECHA_PLAN which doesn't match how you have defined the table. Presumably it is the same as DATE_SERVICE.

As you have coded it, the trigger fails if the entered date is greater than the current date. But when you say ...

I would check that the dates entered
... will be equal to or greater than
the current date is inserted in the
record.

... perhaps what you want is to enforce a rule that the date must be greater or equal to the current date. If so, your trigger ought to fail if the entered date is less than the current date. Like this ...

IF(:NEW.FECHA_PLAN < sysdate )THEN

Note that we can use sysdate directly, so the select ... from dual is unnecessary (unless as Rene points out in the comments you want to use the same value multiple times).

If this doesn't address your problem you will need to explain a bit more.

得不到的就毁灭 2024-08-26 10:24:22

您的日期比较是否与您所说的想要的相反?如果插入的日期大于 SYSDATE,则会引发错误。您是否应该测试 SYSDATE 是否大于新记录中插入的日期?试试这个:

  IF(fecha_ac >= :NEW.FECHA_PLAN)THEN
    dbms_output.put_line('The date of the plan should be more than the current date ');
    raise_application_error(-20601, 'Dato invalido');
  END IF;

Isn't your date comparison backwards to what you stated you wanted? You're raising an error if the date inserted is greater than SYSDATE. Shouldn't you test for SYSDATE greater than the date inserted in new record? Try this:

  IF(fecha_ac >= :NEW.FECHA_PLAN)THEN
    dbms_output.put_line('The date of the plan should be more than the current date ');
    raise_application_error(-20601, 'Dato invalido');
  END IF;
羁〃客ぐ 2024-08-26 10:24:22

如果触发器引发您的自定义 20601 错误,则意味着触发器已成功编译并工作,问题出在您的逻辑中。在你的问题中,如果你想禁止过去或未来的日期还不够清楚。换句话说:如果 fecha 计划必须是未来的,那么运营商就错了。如果它必须是过去的日期那就没问题。

话虽如此,将业务逻辑放入触发器中并不是一个好主意。

If the trigger raises your custom 20601 error, it means that the trigger is compiled successfully and works, and the problem resides in your logic. In your question it is not clear enough if you want to disallow past or future dates. In other words: if fecha plan must be future the operator is wrong. If it must be a past date then it's fine.

With that said, it's not a good idea to put business logic in triggers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文