我需要制作一个PL / SQL类型,只能使用10000000到9999999999999之间的数字(所有正数从8到13个数字)。我正在使用SQL Developer 11g。 到目前为止,我已成功使用:
CREATE OR REPLACE TYPE someType AS OBJECT (
tip NUMBER (13)
) FINAL;
CREATE TABLE someTable(
ID NUMBER PRIMARY KEY,
PIN someType,
check ( PIN.tip between 100000000 and 9999999999999)
)
如何将someTable的check子句放入someType的主体?
CREATE OR REPLACE TYPE someType AS OBJECT (
tip NUMBER (13)
) FINAL;
CREATE TYPE BODY someType as...?
答案 0 :(得分:1)
您可以创建CONSTRUCTOR
,为超出范围的值引发用户定义的EXCEPTION
。
CREATE OR REPLACE TYPE sometype AS object
(
tip NUMBER (13),
CONSTRUCTOR
FUNCTION sometype ( tip NUMBER )
RETURN SELF AS RESULT
) FINAL;
CREATE
OR REPLACE TYPE BODY sometype AS
CONSTRUCTOR FUNCTION sometype
( tip NUMBER )
RETURN SELF AS RESULT AS tip_beyond_range EXCEPTION;
PRAGMA EXCEPTION_INIT(tip_beyond_range, -20101);
BEGIN
IF tip BETWEEN 100000000
AND 9999999999999 THEN
self.tip := tip;
RETURN;
ELSE
raise_application_error(-20101, 'ERROR : TIP SHOULD BE BETWEEN 100000000 and 9999999999999');
END IF;
END ;
END;
现在,下面的第二个调用将导致错误。
DECLARE
t1 sometype;
BEGIN
t1 := NEW sometype(100000000);
t1 := NEW sometype(10000000);
END;
/
ORA-20101: ERROR : TIP SHOULD BE BETWEEN 100000000 and 9999999999999 ORA-06512: at "SQL_ZVRGRVBJDGVKVLZDKPHRMMRDP.SOMETYPE", line 12
ORA-06512: at line 6
ORA-06512: at "SYS.DBMS_SQL", line 1721