Can we pass parameters in triggers in Oracle? - oracle

Like in procedures and Functions can we pass parameters to triggers? Can A trigger can be explicitly called?

An object based trigger is raised by an event's occurence(as update,insert,select)on a specific object of the database. There is also system triggers, fired by system specific events(as shutdown,startup database, user connection etc..).
This is the main purpose of a trigger in databases, you can't raise it explicitly, if you want it to run the only way is to raise the event. Also passing parameters isn't part of trigger definition, but you can handle the event attributes,(which can be passed to the trigger body that may can contain functions or procedures).
I hope that i've responded to your question, can i know what is your need for trying to do that.?

What you can do is to create a table that will store temporarily the data that you want to access in your trigger.
1-Create a table "tmp_data" for instance.
2-Before running the event that will fire the trigger (stored procedure, insert, update...) insert into tmp_data the data that you want to use in the trigger.
3-In the trigger, to access the data you needed, you make a query on the table tmp_data.
4-After have been done with the data, you clean the table tmp_data for the next use.
Hope has been helpful!

Related

How to use Oracle trigger to clone user

I would like to know how to use "oracle triggers" to clone a user or copy all the roles and rights from one user to other.
The procedure should contain two parameter. One source user and other target user.
I wouldn't use a trigger for that. A trigger is a stored program unit that gets executed when one of the following operations occur:
DML statement on a table or view
DDL statement by a particular user
Database event
The list doesn't contain an event that triggers the cloning of a user. Besides, triggers don't have parameters like old or new users.
This overview explains nicely what triggers can do.

Database Triggers and ECA ( Event Condition and Action )

I am new to Triggers in database and ECA.
According to my understanding, Triggers are the automated procedures in PL/SQl that are fired based upon some Event and Condition which is provide by ECA.
But does ECA has its own independent significance in database, it some independent system? or How do Triggers and ECA relate to each other ?
Any help on the above topics is highly appreciated .
Triggers mechanism allows to bind custom procedures to low level DML operations like insert, update and delete. Procedures are created by language used by data base, for example: oracle uses pl/sql, sql server uses tsql and other can use others.
For example:
Developer can create trigger on delete operation on table customers that invoke procedure that create log.
You can say Trigger is fulfilling ECA concept.
Event - is DML operation on data.
Condition - defined during trigger creation.
Action - procedure defined by developer.

Add username into another table while creating a new Oracle user

I am creating a simple application where I am using a Servlet and JDBC to create users.
Is there any way I can use a trigger to insert the username into another table when the Oracle user is created with:
create user xyz ......
Once this query executes successfully, it inserts the user information in the DBA_USERS table.
I want the username to also be inserted into another table. Should I add it to that other table manually through JDBC, or can I create a trigger to do it automatically?
You could use a system trigger based on the create user DDL (but not a DML trigger on the dba_users view - you can't do that anyway, but don't even think about trying to do anything based on the data dictionary). But you wouldn't really have any way for that to know if the user being created is actually an application user - you'd be assuming any user added to the DB could only be related to your application.
Since you have to take other steps anyway - such as granting roles and/or privileges, maybe adding other application security data, etc., it probably makes more sense to do the table insert manually.
It may even make more sense to put all the user-creation code into a stored (and probably packaged) procedure, and just call that over JDBC; the downside of that is that the create user and any other DDL would need to be executed from within the procedure as dynamic SQL. Any of your own table inserts would be together though, and you'd only have that single JDBC call to make.
You can also have other procedures to modify and delete users.
Something to bear in mind, however you do it, is that DDL implicitly commits. Not necessarily a problem, just something to be aware of, so you can order the steps in a recoverable way.

Suggest Event pattern design in database

Can anyone suggest a good event pattern design or framework for changes in table in oracle.
Changes are not just the based on the column value change,but also the business driven logic .All the logging should be driven by some event setup.
At the end we track the changes and drive the business logic based on the changes .
I might be talking too high level,sorry for that :)
Assume TableA needs to be tracked for ColumnA.
Create AUDIT_TRAIL Table with columns TABLENAME, COLUMNNAME, OLDVALUE,NEWVALUE, DATEANDTIME, PK_SEQ
Create a Trigger to poll TableA for any change in ColumnA, and insert them into AUDIT_TRAIL (PK_SEQ should be a Oracle Sequence Number, DATEANDTIME should be from sysdate)
Something like this for trigger
CREATE OR REPLACE TRIGGER trg_table_audit
before insert or update or delete on tableA REFERENCING NEW AS NEW OLD AS OLD
for each row
begin
if inserting or deleting then
insert into audittrail (....)
Reading the fine manual: Using Triggers to Write Audit Data to a Separate Table:
You can use triggers to supplement the built-in auditing features of Oracle Database. The trigger that you create records user actions to a separate database table. When an activity fires the trigger, the trigger records the action in this table. Triggers are useful when you want to record customized information such as before-and-after changes to a table.
Reading even more the fine manual: PL/SQL Triggers:
A trigger is like a stored procedure that Oracle Database invokes automatically whenever a specified event occurs.
On top of that you can build a setup engine that turns triggers on and off. The "business logic" can later read the data recorded by the triggers.
I assume you're not interested in auditing. For the details see e.g. Verifying Security Access with Auditing.
How about you start with using TRIGGERS? And then from there, you could call procedures/functions from a package body for tracking the changes on the table/s.

How to call the Triggers in user deefined way?

I created the Employee table which contains EmpNo,EName,EDesignation as its fields.Also i created the 3 Triggers namely Trigger_1,Trigger_2 and Trigger_3.All the Triggers are Statement level triggers and fired after the update done in the table.Now i want the following orders in which the triggers are going to fired when the update statement is executed.
The Order is
Trigger_3,
Trigger_1,
Trigger_2
Can anyone tell me the way to fire the trigger events in userdefined way?I m using Oracle 9i
Trigger Evaluation Order
Quote from Oracle documentation:
Although any trigger can run a
sequence of operations either in-line
or by calling procedures, using
multiple triggers of the same type
enhances database administration by
permitting the modular installation of
applications that have triggers on the
same tables.
Oracle Database executes all triggers
of the same type before executing
triggers of a different type. If you
have multiple triggers of the same
type on a single table, then Oracle
Database chooses an arbitrary order to
execute these triggers.
Each subsequent trigger sees the
changes made by the previously fired
triggers. Each trigger can see the old
and new values. The old values are the
original values, and the new values
are the current values, as set by the
most recently fired UPDATE or INSERT
trigger.
To ensure that multiple triggered
actions occur in a specific order, you
must consolidate these actions into a
single trigger (for example, by having
the trigger call a series of
procedures).
see also http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
Have one trigger with the contents of the three.
If you cannot, for reasons for modularization, reusability..., create three stored procedures and call these one by one in the single trigger.
Upgrade to 11g and you can define trigger execution order

Resources