Upload
ashtareth87
View
223
Download
0
Embed Size (px)
Citation preview
8/9/2019 Triggers and IO PLSQL
1/28
Triggers
I/O and PL/SQL
Maria Luisa Yu
August 21, 2008
8/9/2019 Triggers and IO PLSQL
2/28
INSTEAD OF Triggers
INSTEAD OF triggers control insert, update,
and delete operations on views, not tables.
They can be used to make nonupdateable
views updateable and to override the default
behavior of views that are updateable.
8/9/2019 Triggers and IO PLSQL
3/28
INSTEAD OF Trigger Syntax
1 CREATE [OR REPLACE] TRIGGER trigger_name
2 INTEAD OF operation
3 ON view name
4 FOR EACH ROW
5 BEGIN
6 ... code goes here ...
7 END;
8/9/2019 Triggers and IO PLSQL
4/28
The INSTEAD OF INSERT Trigger
1 CREATE [OR REPLACE] TRIGGER
trigger_name2 INTEAD OF INSERT
3 ON view name
4 FOR EACH ROW5 BEGIN
6 ... code goes here ...
7 END;
8/9/2019 Triggers and IO PLSQL
5/28
The INSTEAD OF UPDATE Trigger
1 CREATE [OR REPLACE] TRIGGER
trigger_name2 INTEAD OF UPDATE
3 ON view name
4 FOR EACH ROW5 BEGIN
6 ... code goes here ...
7 END;
8/9/2019 Triggers and IO PLSQL
6/28
The INSTEAD OF DELETE Trigger
1 CREATE [OR REPLACE] TRIGGER
trigger_name2 INTEAD OF DELETE
3 ON view name
4 FOR EACH ROW5 BEGIN
6 ... code goes here ...
7 END;
8/9/2019 Triggers and IO PLSQL
7/28
AFTER SUSPEND Triggers
a new type of trigger that fires whenever a
statement is suspended might occur as the result of a space issue such
as exceeding an allocated tablespace quota.
This functionality can be used to address the
problem and allow the stalled operation to
continue.
8/9/2019 Triggers and IO PLSQL
8/28
AFTER SUSPEND Syntax
CREATE [OR REPLACE] TRIGGER trigger_name
AFTER SUSPEND
ON {DATABASE | SCHEMA}
BEGIN
... code ...
END;
8/9/2019 Triggers and IO PLSQL
9/28
Maintaining Triggers
Oracle offers a number of DDL statements that
can help you manage your triggers. You can enable, disable, and drop triggers,
view information about triggers, and check the
status of triggers.
8/9/2019 Triggers and IO PLSQL
10/28
Disabling Triggers
Disabling a trigger causes it not to fire when its
triggering event occurs. Dropping a trigger causes it to be removed
from the database altogether.
Syntax
ALTER TRIGGER trigger_name DISABLE;
8/9/2019 Triggers and IO PLSQL
11/28
Enabling Triggers
A disabled trigger can also be reenabled asshown in the following example:
Syntax
ALTER TRIGGER emp_after_insert ENABLE;
8/9/2019 Triggers and IO PLSQL
12/28
Dropping Triggers
Syntax
DROP TRIGGER emp_after_insert;
8/9/2019 Triggers and IO PLSQL
13/28
Viewing Triggers
DBA_TRIGGERS
All triggers in the database ALL_TRIGGERS
All triggers accessible to the current user
USER_TRIGGERSAll triggers owned by the current user
8/9/2019 Triggers and IO PLSQL
14/28
Checking the Validity of Triggers
Oddly enough, the trigger views in the data
dictionary do not display whether or not atrigger is in a valid state.
If a trigger is created with invalid PL/SQL, it is
saved in the database but marked as INVALID.
You can query the USER_OBJECTS orALL_OBJECTS views to determine this status.
8/9/2019 Triggers and IO PLSQL
15/28
SQL> CREATE OR REPLACE TRIGGER
invalid_trigger 2 AFTER DDL ON SCHEMA
3BEGIN
4NULL
5END;
6/ Warning: Trigger created with compilation
errors.
8/9/2019 Triggers and IO PLSQL
16/28
SQL> SELECT object_name,
2object_type,
3 status
4 FROM user_objects
5WHERE object_name = 'INVALID_TRIGGER';
OBJECT_NAME OBJECT TYPE STATUS------------- ----------- -------
INVALID_TRIGGER TRIGGER INVALID
8/9/2019 Triggers and IO PLSQL
17/28
I/O and PL/SQL
most common mechanisms forI/O in PL/SQL, including the
following built-in packages:
DBMS_OUTPUT
For displaying information on the screen
UTL_FILE
For reading and writing operating system files
UTL_MAIL and UTL_SMTPFor sending email from within PL/SQL
UTL_HTTP
For retrieving data from a web page
8/9/2019 Triggers and IO PLSQL
18/28
Enabling DBMS_OUTPUT
If DBMS_OUTPUT is disabled (default setting),
then calls to the PUT_LINE and PUT programsare ignored; the buffer remains empty.
To enable DBMS_OUTPUT, you will generally
execute a command in the host environment.
SET SERVEROUTPUT ON
8/9/2019 Triggers and IO PLSQL
19/28
Specify that you want the text displayed by
DBMS_OUTPUT wrapped at the SQL*Plus linelength.
This version respects integrity of "words."
SET SERVEROUTPUT ON FORMAT WORD_WRAPPED
8/9/2019 Triggers and IO PLSQL
20/28
Specify that you want the text displayed by
DBMS_OUTPUT wrapped at the SQL*Plus linelength. The wrapping occurs regardless of
word separation. The wrapping occurs
regardless of word separation.
SET SERVEROUTPUT ON FORMAT WRAPPED
8/9/2019 Triggers and IO PLSQL
21/28
Set the buffer size to the maximum allowed
prior to Oracle Database 10g Release 2:SET SERVEROUTPUT ON SIZE 1000000
Set the buffer size to "unlimited" (Oracle
Database 10g Release 2 only):
SET SERVEROUTPUT ON
8/9/2019 Triggers and IO PLSQL
22/28
Write Lines to a Buffer
Call DBMS_OUTPUT.PUT_LINE or
DBMS_OUTPUT.PUT to put information intothe buffer.
PUT_LINE adds a newline marker after its text.
PUT places text in the buffer without a newline
marker.
8/9/2019 Triggers and IO PLSQL
23/28
Specify that you want the text displayed by
DBMS_OUTPUT to be truncated at theSQL*Plus line length; the rest of the text will
not be displayed.
SET SERVEROUTPUT ON FORMAT TRUNCATED
Turn off output in SQL*Plus:SET SERVEROUTPUT OFF
8/9/2019 Triggers and IO PLSQL
24/28
Example
BEGIN
DBMS_OUTPUT.put_line ('Steven');DBMS_OUTPUT.put_line (100);DBMS_OUTPUT.put_line (SYSDATE);
END;
/
8/9/2019 Triggers and IO PLSQL
25/28
Read the Contents of a Buffer
If you need to obtain the contents of the
DBMS_OUTPUT buffer, you can call theGET_LINE
The GET_LINE procedure retrieves one line of
information from the buffer (up to the maximum
supported by DBMS_OUTPUT), and returns astatus value of 0 if successful.
8/9/2019 Triggers and IO PLSQL
26/28
Here's an example that uses this program to
extract the next line from the buffer into a localPL/SQL variable: and GET_LINES procedures.
8/9/2019 Triggers and IO PLSQL
27/28
FUNCTION get_next_line RETURN VARCHAR2
IS
return_value VARCHAR2
(2
55);get_status INTEGER;
BEGIN
DBMS_OUTPUT.GET_LINE (return_value, get_status);
IF get_status = 0
THEN
RETURN return_value;ELSE
RETURN NULL;
END IF;
END;
8/9/2019 Triggers and IO PLSQL
28/28
Reference
Oracle PL/SQL Programming By
Steven Feuerstein,Bill Pribyl ...............................................
Publisher: O'Reilly
Pub Date: August 2005
ISBN: 0-596-00977-1
Pages: 1198