1

PL / SQLのvarchar2変数がトリガーの10文字のようであることを確認するにはどうすればよいですか?そして、それは自動的に挿入を続行しますか?

--trigger that checks that number of characters are 10, doesnt work
create or replace trigger checkthings
before insert or update
on tblTenChars
declare
noGood exception;
begin
if :new.justTenVars(size) <> 10 then --this is not the way? 
raise noGood;
end if;
exception
when noGood then
raise_application_error(-20008, 'Wrong not 10 characters');
end;
4

1 に答える 1

3

トリガーではなく、チェック制約を使用します。

alter table tblTenChars add constraint checkthings
  check (length(justTenVars) = 10);

チェック制約はより単純で効率的です。

ただし、完全を期すために、トリガーコードは次のようになります。

create or replace trigger checkthings
before insert or update
on tblTenChars
for each row
begin
  if length(:new.justTenVars) <> 10 then 
    raise_application_error(-20008, 'Wrong not 10 characters');
  end if;
end;

例外が発生した場合、挿入または更新は中止されます。それ以外の場合は発生します。

于 2011-05-24T11:57:24.227 に答える