[英]create a trigger in oracle which will not allow any insert or update with if condition
I have two tables-- 我有两张桌子
create table mobile
(
id int,
m_name varchar(20),
purchase_date datetime
)
insert into mobile values('1','Samsung corby','12-JAN-12');
insert into mobile values('2','Nokia E5','15-MAR-12');
insert into mobile values('3','Sony Talk','10-FEB-12');
create table location
(
id int,
l_name varchar(20)
)
insert into location values(1,'London');
insert into location values(2,'Washington');
insert into location values(3,'Mexico');
I want to create a trigger which will ensure that sony talk mobile which is in mexico can not be purchased on December.That means whenever I try to insert data in mobile table with ID as 3 and purchase_date as December, the trigger should stop it and give appropriate message. 我想创建一个触发器来确保无法在12月购买墨西哥的Sony Talk Mobile,这意味着每当我尝试将ID为3,purchase_date为12的移动表中的数据插入时,触发器应将其停止并提供适当的信息。
My code for this is -- 我的代码是-
create or replace trigger trg1
before insert or update
on mobile
for each row
declare
var1 number(4);
begin
select id into var1 from location where l_name='Mexico';
IF (:new.id = var1 and to_char(:new.purchase_date, 'MONTH') = 'DECEMBER') THEN
raise_application_error( -20001, 'THIS mobile CAN NOT BE PURCHASED NOW.');
END IF;
end;
The trigger was created, but when i try to insert this data using this code-- 触发器已创建,但是当我尝试使用此代码插入此数据时-
insert into mobile values('3','Sony Talk','10-JAN-11');
The trigger fires but gives an error -- 触发器触发但给出错误-
ORA-04098: trigger 'OPS$0924769.TRG1' is invalid and failed re-validation
and my if code is also not working properly-- 而且我的if代码也无法正常工作-
IF (:new.id = var1 and to_char(:new.purchase_date, 'MONTH') = 'DECEMBER') THEN
raise_application_error( -20001, 'THIS mobile CAN NOT BE PURCHASED NOW.');
END IF;
its not checking for both id and purchase_date. 它不会同时检查id和Purchase_date。 I gave purchase_date as JAN , so in this case the trigger should not fire.
我将Purchase_date设为JAN,因此在这种情况下不应触发触发器。 I am confused.
我很困惑。
show errors
after creating the trigger, SQL*Plus will display the syntax errors to you. show errors
,则SQL * Plus将向您显示语法错误。 DATETIME
data type. DATETIME
数据类型。 The data type of the purchase_date
column in mobile
would need to be a DATE
. mobile
purchase_date
列的数据类型必须为DATE
。 mobile
table mobile
表的错误,您发布的触发器便可以在我的系统上编译并正常运行 TO_CHAR
with a format mask of MONTH
, the results will be right-padded with spaces to the length of the longest month (9 characters). MONTH
TO_CHAR
时,结果将用空格填充到最长月份的长度(9个字符)。 Since DECEMBER
only has 8 characters, your condition is never going to evaluate to TRUE
. DECEMBER
仅包含8个字符,因此您的条件永远都不会求值为TRUE
。 You would need to either TRIM
the output or to use the fmMONTH
format mask which does not pad the output with spaces. TRIM
输出或使用fmMONTH
格式掩码这不垫用空格输出。 Something like 就像是
create or replace trigger trg1
before insert or update
on mobile
for each row
declare
var1 number(4);
begin
select id into var1 from location where l_name='Mexico';
IF (:new.id = var1 and to_char(:new.purchase_date, 'fmMONTH') = 'DECEMBER') THEN
raise_application_error( -20001, 'THIS mobile CAN NOT BE PURCHASED NOW.');
END IF;
end;
would be more likely what you're looking for. 更有可能是您要寻找的东西。 But that doesn't explain why your trigger is getting compilation errors.
但这并不能解释为什么触发器触发了编译错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.