Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using variables in a trigger 1

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
0
0
US
I am working on an update/delete trigger on tblProjects. The idea is to keep track of the project's version by keeping a history in tblProjectHistory.

I am not quite sure how to do this but this is what I have so far:
Code:
CREATE OR REPLACE TRIGGER trg_ProjectHistory
BEFORE UPDATE OR DELETE
ON tblPROJECTS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
declare tmpVersion number;
BEGIN
  select version+1 into tmpVersion from tblPROJECTS
                          where project_id = :old.project_id);

...

Every insert into history table will have previous version + 1. The above code throws an error. It doesn't like
Code:
select version+1 into tmpVersion from tblPROJECTS
                          where project_id = :old.project_id);

I found some info where they were calling functions. Should I write a function or is there a better way of doing this? Thanks in advance.



 
It depends exactly what you are trying to do, which isn't clear from your description. However, you can't select from tblProjects because the table is mutating (do a search for "mutating tables" to get more information on the problem). You can however do something like:

:new.version := :eek:ld.version + 1;

For Oracle-related work, contact me through Linked-In.
 
thanks Dagon.
Sorry for misleading, the version field is actually in the history table. The project table does not have a field called version. I guess if i had that it would be easier. I created a function in my package. This seem to work: (function body)
Code:
 FUNCTION getProjectVersion ( projectID number) 
      return number
      is
        v_version number;
        v_projectID number := projectID;
     begin 
        select max(version) + 1 into v_version
        from tblProject_history
        where project_id = v_projectID;
        return v_version;
     end;

in my trigger:
Code:
 select ICS.GETPROJECTVERSION( :OLD.project_ID ) into   tmpVersion from dual;
not sure if this is the most efficient way of doing.
thanks

 
I'd question why you need to select it from dual. You can just do:

tmp_version := ICS.GETPROJECTVERSION( :OLD.project_ID );

Also, might be worth changing max(version) to NVL(max(version,1)) to cope with the situation where there is no history row.


For Oracle-related work, contact me through Linked-In.
 
my function handles nvl:

Code:
FUNCTION getProjectVersion ( projectID number) 
      return number
      is
        v_version number;
        v_projectID number := projectID;
     begin 
        select nvl(max(version),0) + 1 into v_version
        from project_history
        where project_id = v_projectID;
        return v_version;
     end;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top