Skip to content
Advertisement

Creating a trigger with SELECT Function

Good day everyone,

I’m having trouble making my trigger work. As far as the functionality of the body and how it behaves, it does exactly as I intended for it to behave. However, when I start to fire the trigger, it returns an error in which Triggers should not have a SELECT statement from inside the main body. I’m still fairly new to coding and how to materialize the idea in my head into code. Hopefully someone could point me in a right direction on how change the Trigger I would like to have as a final result. Please see below script.

Update: Expected result would be whenever a user UPDATE a row and INSERT a new record via the application or job being run in the background, S1_HOVER_REPORT column would be updated with the value from the SELECT script and use the data from the S1_HOVER case result.

(Edit: I have updated the details of the problem above, added the Table being used and Error return)

(Update: I have included the updated trigger and it’s now compiling without any issue, but I’m having errors whenever I try updating a record)

Advertisement

Answer

Firstly from the error message

S3 is the veiw ( you are creating the view by doing a select inside an update statment). You can try and change this to have key preservation but I really wouldn’t know how.

The error suggests updating the base tables not the view. So as mentioned in the comments :old and :new are your friend.

:OLD holds all the values of the table the trigger is created on BEFORE the update (null if insert)

:NEW holds all the values of the table the trigger is created on AFTER the update / insert…..

So if I understand what you want to do correctly you would need to…

  1. declare a variable eg v_S1_hover_report

  2. do your select returning whatever value you need into this variable

  3. set the value in your site table by doing

    :NEW.S1_HOVER_REPORT := v_S1_hover_report

By setting this value into the :NEW object when a commit happens it will be committed to the database. This completely removes the need for an update statement in the trigger.

You can also use :NEW.id in your select statement to filter it down to the record you are updating if it is helpfull

OR

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement