Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Create trigger problem with PL/SQL focus on time/date

848561Mar 18 2011 — edited Mar 18 2011
Hello every one,

I am working on a Trigger for my little Oracle XE database.

Here are some informations about my database:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
My table looks like this:
CREATE TABLE TRIGGERTEST
   (
      ORDERID NUMBER(38,0) NOT NULL, 
      STARTDATE TIMESTAMP (6), 
      ENDDATE TIMESTAMP (6), 
      PAYDATE TIMESTAMP (6), 
      CUSTOMERID NUMBER(38,0) NOT NULL, 
      CONSTRAINT TRIGGERTEST_PK PRIMARY KEY (ORDERID)
   );
And now comes my problem :)
- I try to create a trigger, which allows to insert a record into the table
- but only IF the customer (customerid) has no orders
- OR the ENDDATE of his order is > sysdate!
- this means that a customer can only have 1 order in the same time.
You can compare it with the implementing of premium and normal accounts. You have to pay to get a premium account and for this time you cant buy again a premium account (only if the end date (ENDDATE) of your premium account is less than current date (sysdate or localtimestamp).

I'm sorry for my bad englisch and my PL/SQL knowledge are not so good!

I try to create a trigger like this one:
create or replace
TRIGGER proof_custid_trg
BEFORE INSERT ON triggertest
  FOR EACH ROW
BEGIN
  IF :NEW.startdate >= :NEW.enddate THEN
    raise_application_error (-20901, 'Startdate >= Enddate');
  ELSIF :NEW.enddate >= LOCALTIMESTAMP 
        AND :NEW.startdate >= :OLD.startdate OR :NEW.startdate <= :OLD.startdate
        AND :NEW.enddate <= :OLD.enddate OR :NEW.enddate >= :OLD.enddate
        AND :OLD.customerid = :NEW.customerid THEN
   raise_application_error (-20902, 'Customer alredy has a premium Account.');
  END IF;
END;
I know, a lot of you will laugh about me :)) but I just try it :)
The trigger is not realy working! I think he ignoere this part -> "AND :OLD.customerid = :NEW.customerid THEN" which is one of the important.

I will be very happy if some one can help me!!

best regards
nedo
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 15 2011
Added on Mar 18 2011
3 comments
377 views