SQL 触发器 DATEPART 未声明

SQL trigger DATEPART is not declared

我正在尝试创建一个触发器,如果​​当前时间在晚上 10 点到早上 6 点之间,它会提示一条错误消息。这是我写的代码:

CREATE OR REPLACE TRIGGER horarioModificar 
BEFORE UPDATE ON employees
FOR EACH ROW
DECLARE
   horaActual NUMBER:= DATEPART(hour, SYSDATETIME());
BEGIN 
    IF  horaActual < 6 OR horaActual > 22 THEN
        raise_application_error(-20250,'No se puede realizar ningún cambio entre las 22:00 y las 6:00');
    END IF;
END;
/

我收到一条错误消息,指出需要声明 DATEPART(错误代码 PLS-00201)。有人知道我的代码有什么问题吗?

您收到的错误消息 (PLS-00201) 表明您正在 运行ning Oracle。

这是在 Oracle 上 运行 触发代码的新版本:

create or replace trigger horariomodificar 
before update on employees 
for each row 
begin 
    if not extract(hour from systimestamp) between 6 and 22 
        then raise_application_error(
            -20250,
            'no se puede realizar ningún cambio entre las 22:00 y las 6:00'
        ); 
    end if; 
end; 
/

要点:

  • datepart()在Oracle中不存在(这是SQL服务器函数);您可以使用 extract(hour from systimestamp) 获取当前小时数

  • 你真的不需要使用变量,所以我删除了那个

  • 这段代码实际上阻止了23h和6h之间的变化,而不是22h和6h之间的变化;否则,您需要:if not extract(hour from systimestamp) not between 6 and and 21