表结构是
Name Null Type
------------- ---- ------------
T_NO NUMBER
T_NAME VARCHAR2(10)
ENTERING_TIME TIMESTAMP(6)
LEAVING_TIME TIMESTAMP(6)
TO_DATE DATE
触发
create or replace trigger t4
before insert
on t4
for each row
declare
d_entering_time timestamp(6):=to_char('09:00:00AM','HH12:MM:SSAM');
begin
if (:new.entering_time <= d_entering_time) then
raise_application_error
(-20002,'Date of joining cannot be after system date.');
end if;
end;
和我的插入查询
insert INTO t3 (entering_time) values ( TO_date('8:31:51AM','HH:MI:SSAM'))
我收到以下错误:
SQL Error: ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SYSTEM.T3", line 2
ORA-04088: error during execution of trigger 'SYSTEM.T3'
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause:
*Action:
任何人都可以建议我发生错误吗?
答案 0 :(得分:1)
您的代码中似乎存在一些错误,
d_entering_time timestamp(6):=to_char('09:00:00AM','HH12:MM:SSAM');
HH12:MI:SSAM
,而非HH12:MM:SSAM
,MI
是分钟,MM
是月。你可以这样试试,
CREATE OR REPLACE TRIGGER t4
BEFORE INSERT ON t3 FOR EACH ROW
DECLARE
d_entering_time TIMESTAMP :=to_timestamp('09:00:00AM','HH12:MI:SSAM.FF');
BEGIN
IF (:NEW.entering_time <= d_entering_time) THEN
raise_application_error (-20002,'Date of joining cannot be after system date.');
END IF;
END;
插入查询,
INSERT INTO t3 (entering_time) VALUES ( to_timestamp('8:31:51AM','HH:MI:SSAM.FF'));
答案 1 :(得分:1)
试试这个:
CREATE OR REPLACE TRIGGER T4
BEFORE INSERT
ON T3
FOR EACH ROW
DECLARE
SSSSS_ENTERING_TIME NUMBER := 32400;
BEGIN
IF ( TO_NUMBER ( TO_CHAR ( :NEW.ENTERING_TIME,
'SSSSS' ) ) <= SSSSS_ENTERING_TIME )
THEN
RAISE_APPLICATION_ERROR (
-20002,
'Date of joining cannot be after system date.' );
END IF;
END;
INSERT INTO
T3 ( ENTERING_TIME )
VALUES
( TO_TIMESTAMP ( '01/01/2010 8:31:51AM',
'DD/MM/RR HH:MI:SSAM.FF' ) );
注意:我从时间部分提取了总秒数并转换为数字进行比较。因此我使用32400秒,这只是实际上午9点。
在Oracle中,我们可以将日期转换为数字,并以各种方式对它们应用算法。
因此to_char(some_date, 'SSSSS')
将时间元素作为自午夜以来的秒数。