Page 321
6 select avg(price) 7 into Running_Avg 8 from Block_Trade_Log 9 where 10 Stock_Symbol = Stock_Symb and 11 Timestamp >= SYSDATE-3; 12 12 return Running_Avg; 13 13 end; 14 / Function created.
Listing 11.37 contains a modified version of Block_Trade_Log_BI that calls the stored function Get_3_Day_Running_Avg.
Listing 11.37. Modifying the stored function to be called by a trigger.
SQL> create or replace trigger Block_Trade_Log_BI before 2 insert on Block_Trade_Log 3 for each row 4 4 declare 5 5 Running_Avg number; 6 6 begin 7 7 :new.Running_Avg_3_Days := ÂGet_3_Day_Running_Avg (:new.Stock_Symbol); 8 8 end; 9 / Trigger created.
If you've decided that you absolutely don't want a particular trigger, you can drop it with the following statement:
DROP TRIGGER trigger-name;
The variable trigger-name is the name of the trigger to be dropped.
For example, to drop the DELETE AFTER trigger on the Repair Header table, issue the following statement via SQL*Plus:
Page 322
SQL> drop trigger Repair_Header_Delete_After; Trigger dropped.
Sometimes, dropping a trigger is too drastic. Instead, you might want to deactivate a trigger temporarily. You can disable a trigger until enabling it again makes sense. To disable a trigger temporarily, use the ALTER TRIGGER statement
ALTER TRIGGER trigger-name DISABLE;
The variable trigger-name is the trigger to disable.
The following example disables the trigger Repair_Header_Delete_After:
SQL> alter trigger Repair_Header_Delete_After disable; Trigger altered.
To enable a disabled trigger, use the statement
ALTER TRIGGER trigger-name ENABLE;
The variable trigger-name is the trigger to enable.
For instance, you can enable Repair_Header_Delete_After by issuing the following command:
SQL> alter trigger Repair_Header_Delete_After enable; Trigger altered.
Congratulations! You've covered a lot of material in this lesson. The most important concepts in this lesson were as follows:
Page 323
Now that you're familiar with SQL and PL/SQL, it's time to move on to something completely different. On Day 12, "Developer/2000: Introduction to Oracle Forms," you are introduced to the tool that you'll use to build a client/server application.
Page 324
Q Is it possible to create a trigger that will fire if a row is read during a query?
A No, a trigger will only fire for an INSERT, UPDATE, or DELETE. You can use the auditing capability in the Oracle RDBMS to record each query submitted by
a user.Q What is the advantage to using a trigger to check for a condition that is already enforced by referential integrity?
A Some entity-relationship design tools generate triggers that seem superfluous. That is, the generated triggers check for violations of referential integrity and return specific error messages. Without such a trigger, the Oracle RDBMS would return an error message indicating that referential integrity was violated but would not specifically identify the offending columns or relationships. These generated triggers make it easy for both developer and user to determine why the operation was disallowed.
Q Is it always necessary to close a cursor?
A It is if you want to change a value of a variable that is used by the cursor and reopen it. In general, it's good practice to close a cursor when it's no longer needed.
The purpose of the Workshop is to allow you to test your knowledge of the material discussed in the lesson. See if you can correctly answer the questions in the quiz and complete the exercises before you continue with tomorrow's lesson.