Chris Pin
Chris Pin

Reputation: 67

TRIGGER AND COUNT to limit the Count value

i want to create a trigger that will

here is what i did that screws me up

I would like to know is this logically correct? and here are my compiler log errors

This is my required outcome: Meter readers can only read a maximum of 5 meters in any given calendar month

My Reading Table has
StaffID
MeterID
DateRead
ReadinID (PK)

Here is the error text:

Error(5,7): PL/SQL: SQL Statement ignored Error(5,27): 
PL/SQL:ORA-00923: FROM keyword not found where expected 
C:\Users\ChrisPin\AppData\Roaming\SQL Developer\assgn2 sat4.sql Error(5,7): 
PL/SQL: SQL Statement ignored Error(5,27): 
PL/SQL: ORA-00923: FROM keyword not found where expected

Here is the trigger code:

CREATE OR REPLACE TRIGGER LIMIT_5_REDINGS
      BEFORE UPDATE OR INSERT ON reading
    FOR EACH ROW
    DECLARE
    ReadingCount  INTEGER;      --# of depts for this employee
      max_read   INTEGER := 5; --max number of depts per employee.
      BEGIN
      select Reading COUNT(*) into ReadingCount
      from (select *
            from Reading
            where to_char(DateRead, 'YYYY-MM') = to_char(sysdate, 'YYYY-MM'))  
      WHERE STAFFID = :NEW.STAFFID;

    IF :OLD.STAFFID = :NEW.STAFFID THEN
        RETURN;
     ELSE
       IF ReadingCount >= max_read THEN
         RAISE_APPLICATION_ERROR (-20000,'Employees are limited to a max of two departments.');
    END IF;
    END IF;
    END;

Upvotes: 2

Views: 5645

Answers (1)

A.B.Cade
A.B.Cade

Reputation: 16905

It's in this line

select Reading COUNT(*) into ReadingCount 

should be

select COUNT(*) into ReadingCount

Upvotes: 2

Related Questions