NULL 不等于也不不同于任何东西。您应该使用IS NULL 或IS NOT NULL,而不是<> 或=。
类似这样的:
create or replace trigger tr_add_on_audit_table
before insert on lds_consultant
for each row
declare
uname varchar2(30);
begin
select username
into uname
from lds_consultant
where username = :NEW.USERNAME;
if uname is not null then --> this!
insert into audit_table
values(null, null, 'nishan', 'insert', null, null, 'cmd', null, 'LDS_CONSULTANT', 'CONSULTANT_ID',null, null, null);
end if;
exception
when no_data_found then
null;
end;
我包含了异常处理部分,以防 SELECT 不返回任何内容;如果不太可能,请将其删除(或正确处理;我正在做什么(NULL;)。此外,如有必要,请处理其他异常。
另外,我建议您命名要插入的所有列。今天,您知道什么值去了哪里,但一两个月后您就会忘记第三个 NULL 值应该是什么意思。
此外,您说不允许用户输入重复的值 - 好吧,这段代码不会让它发生。
最简单的选择是在USERNAME 列上创建一个唯一键约束,并让Oracle 处理重复项。
如果你想自己做,你应该例如
raise_application_error(-20000, 'Duplicate username is not allowed);
但是,这不会将您的 INSERT 保存到表中,因为所有内容都将回滚。为了解决这个问题,创建一个使用 pragma automatic_transaction 并将插入提交到审计表中的过程。
一切都会像这样:
create or replace procedure p_audit as
pragma autonomous_transaction;
begin
insert into audit_table
values(null, null, 'nishan', 'insert', null, null, 'cmd', null, 'LDS_CONSULTANT', 'CONSULTANT_ID',null, null, null);
commit;
end;
/
create or replace trigger tr_add_on_audit_table
before insert on lds_consultant
for each row
declare
uname varchar2(30);
begin
select username
into uname
from lds_consultant
where username = :NEW.USERNAME;
if uname is not null then
p_audit;
raise_application_error(-20000, 'Duplicates are not allowed')
end if;
exception
when no_data_found then
null;
end;
/
但是,再一次,为什么要打扰?唯一性是这里的关键词。