简体   繁体   English

在oracle中创建一个触发器,该触发器不允许使用if条件进行任何插入或更新

[英]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. 我很困惑。

  1. Your trigger was created with compilation errors. 您的触发器创建时出现编译错误。 If you type show errors after creating the trigger, SQL*Plus will display the syntax errors to you. 如果在创建触发器后键入show errors ,则SQL * Plus将向您显示语法错误。
  2. Oracle does not have a DATETIME data type. Oracle没有DATETIME数据类型。 The data type of the purchase_date column in mobile would need to be a DATE . mobile purchase_date列的数据类型必须为DATE
  3. The trigger you posted compiles and runs without error on my system once I correct the error in defining the mobile table 一旦我更正了定义mobile表的错误,您发布的触发器便可以在我的系统上编译并正常运行
  4. When you use 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM