I have an existing TABLE postn_matrix
which contains a list of employees and a count of their resp. positions in the organisation.
Whenever a position of a user is added or removed, the corresponding count is reflected in the table thro' this trigger (VIA UPDATE)
Now, if there is a new user, he will not have an entry in postn_matrix
, so I have to insert a new record for him/her (VIA INSERT). This needs to be brought in from the BASE TABLE.
The update seems to be working fine but I am not able to bring in a new user into the table.
I've been trying to handle this case with a cursor. But it hasnt been of any help yet.
I'm hoping some expert could show me the light.. :). any other suggestions besides using a cursor will be much appreciated
CREATE OR REPLACE TRIGGER TRIG1
BEFORE INSERT OR DELETE ON (BASETABLE)
FOR EACH ROW
DECLARE
cursor c1 is
select person_id
from postn_matrix;
v_temp varchar2(15);
BEGIN
IF INSERTING THEN
open c1;
LOOP
fetch c1 into v_temp;
if v_temp!=:new.person_id THEN
insert into POSTN_MATRIX (PERSON_ID)
VALUES (:new.PERSON_ID);
else
UPDATE POSTN_MATRIX
//this is working fine ;
END IF;
end loop;
close c1;
END
/
Because of the loop (which is missing an exit clause - hopefully you've just lost that translating this into a question) you're going to attempt to insert a record into pstn_matrix
for every record the cursor returns, whether there are any matching :new.person_id
or not; and if there is match you'll also do the update
. Which probably isn't what you want, and you might get a constraint violation among other things. You also aren't setting your counter field - if that is not nullable then that will error. But you haven't said what errors, if any, you are getting.
If you must do this through a trigger then you can either check if there is a row for the new person at all:
DECLARE
v_temp postn_matrix.person_id%TYPE;
BEGIN
IF INSERTING THEN
select max(person_id) into v_temp
from postn_matrix
where person_id = :new.person_id;
if v_temp is null then
-- no record found, so insert one
insert into postn_matrix (person_id, position_count)
values (:new.person_id, 1);
else
-- record exists, so update
update postn_matrix ...
end if;
...
... or use merge
.
But I don't like this model, and you're setting up the potential for data discrepancies with concurrent modifications to the base table. Trying to maintain a count like this is not necessarily as simple as it seems.
I'd usually prefer to make this a view, which will always be up to date and doesn't need the trigger complicating things:
create view postn_matrix as
select person_id, count(*)
from basetable
group by person_id;
Of course, I may be misinterpreting or oversimplifying what your base table(s) does and what you need postn_matrix
for. It seems a little trivial to have even as a view. If you have separate person
and person_position
tables, say, then you can add in an outer join to see people with no positions:
create view postn_matrix as
select p.person_id, count(pp.position_id)
from person p
left join person_position pp on pp.person_id = p.person_id
group by p.person_id;