Create a trigger after delete
Hi,
I would a distributed database with two database. I have a orders tables on both the database. I would like to create a trigger for when deleting a record from one database would delete from the other database.
CREATE OR REPLACE TRIGGER orders_after_delete
AFTER DELETE ON orders
BEGIN
-- DELETE record into orders table in Database 1 (DB1)
END;
Thanks.
Your trigger is (after) STATEMENT level trigger.
What happens if you delete two (or more) rows using one DELETE statement:
ORA-01427: single-row subquery returns more than one row.
Use "WHERE o_id IN (SELECT...)"
instead of "WHERE o_id = (SELECT...)".
Another way is to use simple ROW level trigger:
CREATE OR REPLACE TRIGGER orders_after_delete
AFTER DELETE ON orders
FOR EACH ROW
BEGIN
DELETE orders@DB1Link
WHERE o_id = :OLD.o_id;
END;
/But, with row level trigger, one local DELETE statement launches more remote DELETE statements (one for each deleted row) .
This can cause performance and/or integrity problems (if table has self-referential integrity constraint, for example).
Regards,
Zlatko
Similar Messages
-
Creating a Trigger for Deleting the records from a parent Table
I am new to creating Trigger
We will need several small tables that will be used to store any records that are deleted by the owner of the table. These will likely need a trigger where we would Delete from the parent table and on that Delete populate the child table with the previous record's data.
Please give me a pseudo code for this
Thanks
John
Edited by: user10750995 on Dec 30, 2008 9:06 AMSomething like this:
CREATE OR REPLACE TRIGGER trg_my_table_hist
AFTER DELETE
ON my_table
FOR EACH ROW
BEGIN
INSERT INTO Hist_MyTable
( column1, column2, ..., DELETION_DATE)
VALUES
(:OLD.column1, :OLD.column2, ...., SYSDATE);
END;
/My_Table is your main table. When a row is deleted, the trigger will be fired and copy the deleted row to another table called Hist_My_Table. I'm supposing that the history table has all columns as they are defined in main tables plus a column named DELETION_DATE.
My experience indicates that, probably, it's a good idea maintain update history and the user. But it depends on your requests.
Regards,
Miguel -
Custom Trigger after Delete not executing
I have a custom trigger that I want to execute after a user performs a DELETE Transaction. Here is the PHP code:
$DB_SERVER = "somewhere.com";
// username:
$DB_USER = "me";
// password:
$DB_PASS = "password";
// database name:
$DB_NAME = "training";
$sessid=$_GET['sessid'];
$userid=$_GET['id'];
mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$sql = "DELETE FROM tms_registered where eid ='$sessid' AND uid = '$userid'";
$sql2 = "SELECT COUNT(*) AS max FROM tms_registered WHERE eid ='$sessid'";
$result = mysql_query( $sql2 )
or die ( 'Unable to execute query.' );
$max_value = mysql_result($result, $i, "max");
$sql3 = "UPDATE tms SET cur_reg='$max_value' WHERE tmid='$sessid'";
I have also written the trigger by declaring the variables at the top of the PHP script page and then use 'global': at the begining of the trigger:
global $DB_SERVER, $DB_USER, $DB_PASS, $DB_NAME, $sessid, $userid;
The trigger does not execute after the DELETE transaction. What am I doing wrong?
TIADear Karthi & Santosh,
Thanks for your prompt replies.
I am already maintaining following detail in OPJF
01 5 S 1 Production 4 Released (general)
Dear Santosh,
I have changed the Syst.Sts from REL to CNF and save it. Deleted the trigger point assignment and reassign it. Create new Production Order. But Now
Trigger is not executing at the time of order creation as well as Order confirmation (CO11N).
There are following functions available in the system:
Release succeeding operations
Release up to stop indicator
Release preceeding operations
Create order with reference
Insert reference operation set
Start workflow task
and i am using "Create order with reference" is it OK, or I have to use another function.
Please suggest.
With thanks,
DSC -
Create a trigger/stored procedure to set one column equal to another
Hey guys,
I have two columns in a table, and when column A is updated or inserted, I need the database to automatically update column B (in the same table) to be equal to that. I can't create a trigger AFTER INSERT OR UPDATE, because it's the same table and it gives me the mutating tables error.
Any help is appreciated.How have you defined your trigger? It sounds like you should just be able to do
BEGIN
:new.columnB := :new.columnA;
END;I assume if you are getting a mutating table error that your trigger is trying to query the table. I don't see a need to query the table.
Justin -
:NEW cannot be used in After Delete Trigger ?
Hi,
Is there any way to get the :NW.value in the After delete trigger for each row. My requirement is audit log of the end user DML operations along with user Name (HERE THE USER IS NOT THE ORACLE USER, BECAUSE OF THE LARGE NUMBER OF END USERS WE ARE MAINTAINING ONE TABLE TO CREATE USER NAME & PASSWORD, WHEN THE USER LOGIN TO ORACLE FORM SCREEN, ASSIGN THE USER NAME TO GLOBAL VARIABLE) & Action Date.
Here is my code for trigger - It is working fine with INSER & UPDATE but for DELETE User is NULL
CREATE OR REPLACE TRIGGER Tgr_stud_det
AFTER INSERT OR UPDATE OR DELETE ON student_details
FOR EACH ROW
DECLARE
BEGIN
IF Inserting THEN
-------------INSERT VALUE---------------
INSERT INTO Log_student_details
(Seq,
App_User,
Action,
Action_Date,
stud_name,
stud_age,
stud_sex)
VALUES
(stud_sequence.NEXTVAL,
:NEW.App_User,
'INSERT',
SYSDATE,
:NEW.stud_name,
:NEW.stud_age,
:NEW.stud_sex);
-------------DELETE VALUE---------------
ELSIF Deleting THEN
INSERT INTO Log_student_details
(Seq,
App_User,
Action,
Action_Date,
Comment_Up,
stud_name,
stud_age,
stud_sex)
VALUES
(stud_sequence.NEXTVAL,
:OLD.App_User,
'DELETE',
SYSDATE,
NULL,
:OLD.stud_name,
:OLD.stud_age,
:OLD.stud_sex);
ELSIF Updating THEN
-------------UPDATE VALUE---------------
INSERT INTO Log_student_details
(Seq,
App_User,
Action,
Action_Date,
Comment_Up,
stud_name,
stud_age,
stud_sex)
VALUES
(stud_sequence.NEXTVAL,
:NEW.App_User,
'UPDATE',
SYSDATE,
'NEW VALUE',
:NEW.stud_name,
:NEW.stud_age,
:NEW.stud_sex);
INSERT INTO Log_student_details
(Seq,
App_User,
Action,
Action_Date,
Comment_Up,
stud_name,
stud_age,
stud_sex)
VALUES
(stud_sequence.CURRVAL,
:NEW.App_User,
'UPDATE',
SYSDATE,
'OLD VALUE',
:OLD.stud_name,
:OLD.stud_age,
:OLD.stud_sex);
END IF;
EXCEPTION
WHEN OTHERS THEN
NULL;
END Tgr_stud_det;
Thanks in advance.Rizly,
As i mentioned in the above post, you should remove the references of :old and :new when you are trying to use the global variables. These values are only significant when you the talk about the record in the table.
For the scenario, you explained, your trigger would insert two records....The trigger would be fired twice.. once during the insert and once during the delete. The audit table will have two records indicating both the actions..
Take a look at this example below...I am artificially manufacturing a user id in the package test_pkg and using that in the insert trigger. As i explained above, you dont need the :old and :new references because the user id is not a column in the table . hence the :old and :new references have no relevance.
Also note that, for the delete, I use the :old value and for the insert, I use the :new value.
for update, I assume you want to store the old record and hence used :old (you can of course use :new too..technically.).
I don't have access to a forms environement, but the user id logic should be similar to what I described below.
sql> create table t(
2 id number,
3 name varchar2(20)
4 );
Table created.
sql> create table t_audit
2 ( id number,
3 name varchar2(20),
4 action varchar2(20),
5 user_id varchar2(20)
6 );
Table created.
sql> create or replace package test_pkg as
2 function get_user_id return varchar2;
3 end test_pkg;
4 /
Package created.
sql> create or replace package body test_pkg as
2 function get_user_id return varchar2 is
3 begin
4 return 'USER' || to_char(sysdate,'HH24:MI');
5 end get_user_id;
6 end test_pkg;
7 /
Package body created.
1 create or replace trigger trg_biud_t
2 before insert or update or delete on t
3 for each row
4 begin
5 if INSERTING then
6 insert into t_audit values (:new.id, :new.name, 'INSERT',test_pkg.get_user_i
7 elsif UPDATING then
8 insert into t_audit values (:old.id, :old.name, 'UPDATE',test_pkg.get_user_i
9 elsif DELETING then
10 insert into t_audit values (:old.id, :old.name, 'DELETE',test_pkg.get_user_i
11 end if;
12* end;
sql> /
Trigger created.
sql> select * from t;
no rows selected
sql> select * from t_audit;
no rows selected
sql> insert into t values (100, 'Rajesh');
1 row created.
sql> insert into t values (200,'Kumar');
1 row created.
sql> delete from t where id = 200;
1 row deleted.
sql> commit;
Commit complete.
sql> select * from t
2 /
ID NAME
100 Rajesh
sql> select * from t_audit;
ID NAME ACTION USER_ID
100 Rajesh INSERT USER15:36
200 Kumar INSERT USER15:36
200 Kumar DELETE USER15:37 -
Need help creating trigger with delete capabilities
I have two tables:
create table person(userid integer,
lname varchar(20),
fname varchar (20),
phone_number varchar(12),
email_address varchar(35),
city varchar(20),
zip_code varchar(20),
department varchar(20),
major_street varchar(20),
cross_street varchar(20),
notes varchar(100),
Primary Key (userid));
create table vehicle(car_id integer,
make varchar(15),
model varchar(20),
year integer,
gas_mileage integer,
capacity integer,
owner integer,
primary key (car_id),
foreign key (owner) references person (userid));
I am trying to create a trigger that would delete all those records in vehicle whenever someone deletes a record in the person table. Here is the code that I run but it get a compilation error. Here is the the trigger text followed by the error:
CREATE OR REPLACE TRIGGER ON_DELETE_PERSON
AFTER DELETE ON person FOR EACH ROW
BEGIN
DELETE from vehicle
where owner=userId;
END;
2/4 PL/SQL: SQL Statement ignored
3/19 PL/SQL: ORA-00904: "USERID": invalid identifier
Any suggestions?CREATE OR REPLACE TRIGGER ON_DELETE_PERSON
BEFORE DELETE ON person FOR EACH ROW
BEGIN
DELETE from vehicle
where owner=:old.userId;
END; -
I created an alias precisely because I wanted to make it the new default name and then quickly realized it wasn't possible. How long after deleting the alias will it become available again so that I can create a new account with it?
I'm trying to make iCloud mail my primary email but I'm concerned that I may have lost the perfect email address forever.I have the same problem. After our wedding I've created an alias ([email protected]) to my actual AppleID Account ([email protected]). Now I'd the idea to delete the alias and my actual AppleID to create a new AppleID with ([email protected]).
Is there really no possibilty to do this?
Thanks in advance for quick and positive feedback. -
If oracle can create trigger after commit???
In my problem, I want to refresh a materialized view after I update a field in the table. But in the trigger "after update" on this table, the materialized view only refresh the old data. It maybe the problem of uncommit. So I want to know if I can refresh the materialized view on the trigger after the table has updated?
Thank you for your advice. Yes, you're right, that's what I thought. To submit a job may result in the differ data sometimes. But if any exception cause in the trigger, I can raise RAISE_APPLICATION_ERROR, and prevent the job submitting. Is it right?
Another question, I don't know how to understand the words you said, "If you're dealing with more than a handful of rows, that's going to kill performance, particularly if the data changes with any regularity.". Why it can kill the performance, and what's the performance you refer to. Can you explain it for me?
And my materialized view is as following:
create materialized view log on t_equipment
with rowid (ereq_code,ereq_parent_equipment,ereq_number1,ereq_boolean2)
including new values
create materialized view log on EQUIPMENT_ATTRIBUTE
with rowid (ETAT_VALUE_NUMBER,COMMONKEY,INDX)
including new values
create materialized view MV_EQUIPMENT
pctfree 0 tablespace TEST
storage (initial 1m next 1m pctincrease 0)
--refresh fast on commit
as
select a.ereq_code,
a.ereq_parent_equipment,
nvl(a.ereq_number1,0) ereq_number,
nvl(c.ETAT_VALUE_NUMBER,0) etat_number,
a.ereq_boolean2
from t_equipment a,EQUIPMENT_SPECS b,EQUIPMENT_ATTRIBUTE c
where a.ereq_code=b.ETES_EQUIPMENT(+)
and b.PK_EQUIPMENT_SPECS=c.COMMONKEY(+)
I still can't add "refresh fast on commit" on it. Why? -
Files missing from desktop after deleting a user profile that was created on there.
I downloaded and installed firefox and it asked me to create a user profile, but it could not save in the default location. I chose to create the profile on the desktop as a test and that worked. However, after deleting that user profile, all of the items on my desktop were removed and cannot be located. The files are not in my trashbin and when I do a search, it says the file cannot be located. I have Windows 7.
You shouldn't create a profile and choose an existing folder.<br />
You should always specify an empty folder and if you didn't do that then you should never remove the files when you remove such a profile because all files in it will be removed and not just the files created by Firefox.
See:
*http://kb.mozillazine.org/Files_created_on_startup -
Create a trigger file after successfully processing of a regular file?
Hi,
I have 10 differet proxy-tofile scenario interfaces going to one target system (10 different transcational data sets). For each interafce after creating the file (fixed file name file1.txt), PI has to create/append a file called "trigger file" which has different structure with fine name, file creation date/time etc. and kind of file (all or delta) which comes from proxy.
So if file1.txt (all) has created, after that PI has to create "trigger file" with:
file1.txt 20110106 all
So if file2.txt (changes), another interface with different transaction, is created, we need to append "trigger file" with:
file1.txt 20110106 all
file2.txt 20110106 delta
Can I do this scenario without BPM, in Integrated Configiration, we are in PI 7.1 EHP1.
Regards,
N@v!nHi All,
Let me try to put the question differently for the same scenario. I have completed dev with two message mappings, two operation mappings (one for regular file and one for tigger file). In ID, I am using the Integrated Configuration (with multiple receivers; here I created 2 communication components, one for regular file, another one for trigger file). This scenario works fine, creates regular file and trigger file. Only thing is it creates trigger file even regular file fails, we dont want to create a trigger file when the regular file creation fails..... since we have lof of scenario like this, we want to use integrated configuration only......I have tried to use one operation mapping (with multiple message mappings with in it), but looks like Integrated Configuration doesn't support this.
So if I have one operation mapping with 2 message mappings in it, does operation mapping fail when one of the message mapping fails? At the end all we wanted to do create a file when ever the regular creats successfully. Pleae help me with the design. Let me know if you need more info.
Thank You,
N@v!n -
Create a trigger to check inserted date is before or after SYSDATE
Hi,
I am trying to create a trigger that will check an inserted date against SYSDATE and alter the value (i.e. make it SYSDATE) when the entered date is incorrect.
For example, I have a Customer table with a record named MemberDate which, when a date is entered to it, will be checked by the trigger to ensure it is not before the current date.
My code so far;
CREATE OR REPLACE TRIGGER Customer
BEFORE INSERT ON
Customer
FOR EACH ROW
DECLARE
e_invaliddate_ud EXCEPTION;
BEGIN
IF :NEW.MemberDate < SYSDATE THEN
RAISE e_invaliddate_ud;
END IF;
EXCEPTION
WHEN e_invaliddate_ud THEN
DBMS_OUTPUT.PUT_LINE('The date entered for MemberDate is invalid. The
MemberDate has been set to the current date');
END;
So far, this trigger only returns an error message but allows the date to be entered even if it is incorrect. I have tried a few ways of altering the entered date to SYSDATE but to no avail.
I am running scripts using SQL PLUS and inserting data using
INSERT INTO Customer
VALUES(CustomerID, ..., MemberDate);
Where the values entered will be checked to ensure the MemberDate is not before SYSDATE.
The MemberDate data-type is DATE and has a rule that it must not be before the current date. I am aware that a constraint may not be used to perform a check using SYSDATE, this is why I am trying to make a trigger.
However, my knowledge of triggers is limited
If anyone can help, I will be very Happy.
Thank you in advance.Use this if you want an error message that halts the execution:
create or replace trigger customer
before insert on customer
for each row
begin
if :new.memberdate < sysdate
then
raise_application_error(-20000,'The date entered for MemberDate is invalid.');
end if;
end;Use this if you want to automatically to overwrite the MemberDate with sysdate without an error message:
create or replace trigger customer
before insert on customer
for each row
begin
:new.memberdate := greatest(sysdate,:new.memberdate);
end;Note: untested
Regards,
Rob. -
Emergency disk full error, doesn't create room after deleting files
Oh, this seems like it could be bad. I got the error and even after deleting 800 MB of large files, I can't even download a 2.4 MB utility file to help figure this out.
What can I do?
I can't even upload a file to my server...!1. Deleted hundreds of MB of my files.
2. Emptied the trash, yes.
3. Restarted.
4. Still can't copy 2 MB of data, says it's full.
I don't know where to look to check system resources or where to find any resident small utility file that could discover enormous files written in error to the disk (my guess). I was a Windows person in another, but recent, life.
Thanks for any help while I can still communicate...
GIna
P.S. Time Machine is running now with no error, so perhaps I'll have a full backup. Disaster for tomorrow noon deadline. -
GeoRasterException: -13463 .. even after creating DML trigger
Hi all
i am getting the following exception :
oracle.spatial.georaster.GeoRasterException: -13463;GeoRaster object with rasterId = 281, rdt = null not found in system data view.
Please create DML trigger for its table and column. Do:
sdo_geor_utl.createDMLTrigger('tableName','columnName')
at oracle.spatial.georaster.JGeoRaster.getRasterSubset_NoComp(JGeoRaster.java:1001)
at oracle.spatial.georaster.JGeoRaster.getRasterSubset(JGeoRaster.java:798)
at oracle.spatial.georaster.JGeoRaster.getRasterImage(JGeoRaster.java:2510)
at oracle.spatial.georaster.JGeoRaster.getRasterImage(JGeoRaster.java:2038)
at spdb.SpatialQuery.getRaster(SpatialQuery.java:68)
at spdb.SpatialQuery.main(SpatialQuery.java:91)
I have created the DML trigger using
sdo_geor_utl.createDMLTrigger('IMGTABLE','RASTER');
but still i keep getting the same error
what am i doing wrong
THanksAB,
about your application, please consider these:
If your original images are seamlessly mosaickable, it's better to mosaick them into one large image and then query. Note, the requirement from the oracle 10g sdo_geor.mosaic is very strict.
But I guess, in most apps, this is not practical. So, you have to query multiple images and do some work in your app. The approach could be:
1. Since your images are all georeferenced, call sdo_geor.generateSpatialExtent and populate the spatial extent for all images. Then build a spatial index (RTree) on the georaster table based on the spatial extents. Note, If the georaster objects have different model spaces, you need call sdo_cs.transform to convert the extents to the same SRID. You can also directly update the spatial extents using a right/appropriate geometry.
2. Spatially query the GeoRaster table using your area-of-interest geometry in a single sql stmt to filter out all the georaster objects which overlap or partially overlap your AOI.
3. call getRasterSubset on each georaster object of the above result set with a proper window.
4. the BLOBs returned by getrastersubset don't have any metadata inside them, but you can easily and precisely figure out their dimension sizes, cell depth, interleaving etc based on the metadata of the georaster objects as well as your query criteria (i.e., band numbers, window sizes and pyramid levels).
5. get all blobs into your app. then you can either directly work on them, such as mosaick them into one image for display, OR you can generate RenderedImages (very easy to do thru JAI) and directly display them into one image (ie, simply render the images in a window) or call JAI rotate/tranform/mosaic to create a single image.
The above approach has been done in many of our partner products, such as PCI FOCUS and Geomatica, Leica ERDAS Imagine and LPS.
In addition, Oracle Applicattion Server MapViewer has this capability as well, which you might consider to use.
Hope this helps.
Best Regards,
Jeffrey -
How to trigger a 'delete' across different related tables in ABAP?
Hello All,
I am creating database tables for storing different values of features coming under different countries. I have eight tables in my design and there are fields repeating in different tables, which i am connecting through foreign key relationship.
After entering values to all my tables, if i need to delete a particular field value which is repeating in more than two tables, is it possible to trigger a delete event by which if am deleting the field value in a particular table, all the related field values also get deleted from the respective tables?
For example, if i have a PRODUCT field in three tables, and if one of the PRODUCT is deleted from a particular table, can i trigger a delete event by which i can delete all the values related to that particular PRODUCT from all the related tables? The PRODUCT is a primary keyfield, and i have maintained proper foreign key relations also.
I have tried deleting entries using the views and using the database table itself, but only that particular table value is being deleted.
Is there any function module for triggering a delete?
Can anyone help me with some solution?Sample code will be helpful.
Thanks in Advance,
ShinoHi,
My friend it is not advisable to delete any field from a database table bcoz it's creats incosistance data for all the other related tables, but there r few Function Modules that provide u this functionality.
Such As:
G_REPORT_DELETE_ADDFLD_ENTRY
G_CATT_DELETE_TABLE_ENTRY
RKE_DELETE_FIELDS_FROM_TABLE
Regards -
Help me in creating a Trigger for Insert and Update Options
Hi
Please help me in creating a Trigger .
My requirement is that after insert or update on a Table , i want to fire an event .
I have started this way ,but doesn't know how to fully implement this .
say i have a dept table
CREATE TRIGGER DepartmentTrigger
AFTER INSERT ON Dept
BEGIN
INSERT INTO mytable VALUES("123","Kiran");
END DepartmentTrigger;
Please tell me how can i put the Update option also .
Thanks in advance .Please tell me how can i put the Update option also .Add "Or Update". ;-)
Here are a few suggestions, but you definitely need to refer to the manual page that the previous poster suggested.
CREATE OR REPLACE TRIGGER DepartmentTrigger
AFTER INSERT Or Update ON Dept
BEGIN
INSERT INTO mytable VALUES(:new.Dept,'DEPT ADDED OR CHANGED');
END DepartmentTrigger;
The "Or Replace" means you can replace the trigger while you're developing without having to type in a drop statement every time. Just change and rerun your script, over and over until you get it right.
Adding "Or Update" or "Or Delete" makes the trigger fire for those events too. Note, you may want seperate triggers in different scripts and with different names for each event. You have to decide if your design really does the same thing whether it's an insert or an update.
:new.Dept is how you would refer to the changed vale of the Dept column (:old.Dept is the prior value). I changed the double quotes on the string in the VALUES clause to single quotes.
Andy
Maybe you are looking for
-
My problem began when I wanted to change my screen saver. At first, I would try to take pictures and download images and they would toatally disappear. So then I uninstalled my icloud, deactivated my photostream, and supposedly restored my phone to
-
Rendering error while writing to file...Quicktime
Hi all, I am brand new here and have an urgent question. I have been getting an error in After Effects CS6 whenever I try to render to a quicktime movie that says "Rendering error while writing to file...An output module failed. The file may be damag
-
Driver compatibility on Win 8.1
Hi all. I have this a DV6 laptop that I've just formatted with Win8.1 pro 64bit http://h10025.www1.hp.com/ewfrf/wc/softwareCategory?os=4063&lc=it&cc=it&dlc=it&sw_lang=&product=4049... Above ther's a list of availlable drivers with Win7 compatibility
-
What if the name of the i-pad (retinal display) adaptor for photos of deer? Husband's 60th soon, need the gift! Thanks
-
How can i get to know what is the reason for implausible meater reading?
Hi All, I am new to this SAP -ISU. can any body tell me how can i get the reason for implausible meater reading. my requirement is to get the unbilled meters becasuse of unrealibility of meter reader. here what i understood is if meater reading is no