ORA 02436 sysdate oracle
這是網上能夠搜索到的比較可信的結果:
Unfortunately you can't use SYSDATE in a check constraint as it is a dynamic Oracle variable. A better way to enforce this constraint would be to use a trigger. For example:
CREATE OR REPLACE TRIGGER trg_emp_dob BEFORE INSERT OR UPDATE ON emp
BEGIN
IF :new.dob > SYSDATE THEN
RAISE_APPLICATION_ERROR (num => -20000, msg => 'DOB Cannot be in the future');
END IF;
END;
不過嘿嘿,我可不信邪,萬事問大哥啊,於是在asktom上找到了這個解決方案:
Just create an additional column using as default SYSDATE. Then you can use this column to compare.
And if you want to hide this column create a view around it...
SQL> create table t
2 (x date,
3 y date default sysdate,
4 constraint x_y check
5 (
6 x >= y
7 )
8 );
Table created.
SQL>
SQL> create view v as select x from t;
View created.
SQL>
SQL> insert into v values (sysdate);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> insert into v values (sysdate-1);
insert into v values (sysdate-1)
*
ERROR at line 1:
ORA-02290: check constraint (ADBM.X_Y) violated
總之oracle不能非常漂亮的解決這個問題,這裏兩個方案你看着選吧。
Unfortunately you can't use SYSDATE in a check constraint as it is a dynamic Oracle variable. A better way to enforce this constraint would be to use a trigger. For example:
CREATE OR REPLACE TRIGGER trg_emp_dob BEFORE INSERT OR UPDATE ON emp
BEGIN
IF :new.dob > SYSDATE THEN
RAISE_APPLICATION_ERROR (num => -20000, msg => 'DOB Cannot be in the future');
END IF;
END;
不過嘿嘿,我可不信邪,萬事問大哥啊,於是在asktom上找到了這個解決方案:
Just create an additional column using as default SYSDATE. Then you can use this column to compare.
And if you want to hide this column create a view around it...
SQL> create table t
2 (x date,
3 y date default sysdate,
4 constraint x_y check
5 (
6 x >= y
7 )
8 );
Table created.
SQL>
SQL> create view v as select x from t;
View created.
SQL>
SQL> insert into v values (sysdate);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> insert into v values (sysdate-1);
insert into v values (sysdate-1)
*
ERROR at line 1:
ORA-02290: check constraint (ADBM.X_Y) violated
總之oracle不能非常漂亮的解決這個問題,這裏兩個方案你看着選吧。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.