是否可以通过类型构造函数在插入/更新之前验证数据?我已经在表格中使用了类型化的对象。需要知道当我在同一设备上插入/更新数据时是否可能在应用程序上出现错误...
这是概念代码...
-- Type Declaration:
create or replace type sampleType as object
(
col1 number(2),
col2 number(2),
col3 number(2),
constructor function sampleType return self as result
) not final;
-- Declaring constructor body:
create or replace type body sampleType as
constructor function sampleType return self as result is
inconsistant_data exception;
pragma EXCEPTION_INIT(inconsistant_data, -660);
BEGIN
dbms_output.put_line('Inside contrctor, before condition');
if self.col1 + col2 != col3 then
dbms_output.put_line('Raising exception');
raise inconsistant_data;
else
dbms_output.put_line('Inside contrctor, returning...');
return;
end if;
dbms_output.put_line('Inside contrctor, after condition');
exception when inconsistant_data then
raise_application_error(-20001, 'col3 should be equal to col1 + col2');
raise;
END;
end;
-- Using it in Table:
create table sampleTable (objSample sampleType);
-- Inserting values:
insert into sampleTable values (new sampleType(1, 1, 1));
这里的最后一条语句插入一条记录。它实际上应该返回一个错误。需要澄清和指导以显示应用程序错误并在不插入记录的情况下拖拽。
提前致谢。