Oracle Trigger with update view and after insertion of rows - oracle

I want to fire a trigger whenever I insert/delete rows in 2 tables belonging to two different schema. I have a sql query as:
select n.name as name, count(distinct d.projects) as count
from emp n, dep d
where n.dep=d.dep
group by n.name;
When I insert or delete rows from these tables emp and dep, I want to fire a trigger automatically to update view containing the above sql query.
I am using Oracle Database. Please advise me how to do that.

Related

How to create a temporary table/view inside an Oracle procedure?

I am making a new procedure making queries to a huge table.
The structure of my procedure is as follows:
{
open cursor for
QUERY 1
UNION
QUERY 2
UNION
QUERY 3
}
The structure of QUERY 1 is INNER JOIN 2 ([INNER JOIN 1 (TABLE) x (TABLE) ] x TABLE )
The structure of QUERY 2 is INNER JOIN 3 ([INNER JOIN 1 (TABLE) x (TABLE) ] x TABLE )
Is there a way to store [INNER JOIN 1 (TABLE) x (TABLE) ] somewhere so that I don't have to do it twice?
EDIT: Forgot to add that I cannot create a table outside of the procedure because multiple instances of this procedure will run in parallel. They will just block each other from running by inserting in the same table. Also, I don't know how many instances will run in parallel so I cannot create as many tables as instances.
Don't create any tables from PL/SQL. It is possible (hint: dynamic SQL), but that's not how Oracle works.
If you need a table, then create it BEFORE running this procedure, either using CREATE TABLE (and name all columns you need), or using CTAS (Create Table As Select) which would - basically - be your current query.
That table can be "normal" or "global (or private, depending on database version) temporary table" (GTT). If you use a GTT, only you can see data stored within. If it is a "normal" table, everyone sees data so you might need to pay attention to who sees & uses what.
Another option is to use the CTE (Common Table Expression, a.k.a. the WITH factoring clause) which can be used directly in the procedure as
with your_view as
(select ...
from table1 join table2 on ...
join table3 on ...
)
select whatever
from some_other_table join your_view
where ...
union
select whatever_else
from yet_another_table join your_View
where ...
[EDIT, after seeing your edit]
If you don't want to use a CTE for some reason, then a GTT might be your choice. Why? See my 3rd paragraph ("everyone sees only their own data").
You could always use a Global Temporary table:
https://oracle-base.com/articles/misc/temporary-tables#temporary-tables

How to force oracle to use index or ordered hints for remote joins

I'm using Oracle 11g. I have a query that joins local table with remote tables using db links. I want the driving table to be the remote table as I primarily filter using remote table to get a few rows. I then want to join them with local table.
The problem is the optimizer ignores ORDERED and INDEX hints and does a full table scan of the local table. I am using the right indexes and have generated statistics. I run the queries individually with each table they use the correct indexes, but with the join, the local table always does a full table scan and acts as the driving table.
SELECT /*+ INDEX_RS_ASC(l) */
*
FROM remote_table#mylink r
JOIN local_table l USING (cont_id)
WHERE r.PRIME_VENDOR_ID = '12345'

Insertions data into multiple tables using triggers

I have 4 tables i.e, A,B,C,D and A is parent table and b & C are child tables.For a tuning query,I am creating another table
i.e., D.In this table some fields of B & C.If any CRUD operations occurs in those two table I want insert/update into d table.So I trying to
append D table insertation in those table triggers.
CREATE OR REPLACE TRIGGER A_ADUIT_TRIGGER
BEFORE INSERT OR DELETING on per
REFERENCING NEW AS NEW OLD AS OLD
IF (INSERTING) THEN
INSERT INTO D(ID) VALUES(:OLD.ID);
ELSIF (DELETING) THEN
INSERT INTO A_AUDIT(ID,ID_NAME,TEST) VALUES(OLD.ID,OLD.ID_NAME,OLD.TEST);
END IF;
Its compiled and remaining tables trigger not included "D" TABLE inseration. When i am trying test at my application side and the logic
insert/update in table "A","B","C" and got expection that is B.ID Cannot insert null.But i have ID Value.I am suspected the issue at
insert of D table.If i remove insertation on table d in trigger and its working fine.Is it possible multiple table handled in triggers
Please help on this.

Two DMLs based on Same Subquery

I need to cancel some orders, and then insert a row into another table, both based on the same subquery.
There is a very small chance that the subquery will return different rows between the time that the 1st and 2nd DMLs are issued.
But is there a proper way to do this such the orders updated are the same orders that are inserted into the cancellations table?
I am using Oracle and JDBC. Thanks.
update orders
set status = 'cancel'
where order_number in (select order_number from some_other_table_where...)
insert into order_cancellations
select order_number select order_number from some_other_table_where...
Here are five approaches that spring to mind:
(1) Put a trigger on the orders table so whenever the status is set to 'cancel', a row is inserted into order_cancellations.
(2) Use the returning clause in insert. Do the insert first and use this information for the update.
(3) Add a creation date to order_cancellations and do this insert first. Then update orders using the just-inserted rows
(4) Wrap the two statements in a transaction (this might require locking the other tables).
(5) Load the subquery data into a temporary table and use that table for both operations.
I also wonder if you could eliminate the need for the cancellations table just by having a cancellation_date column in the orders table.

Updating a SQL table where items to change are identified in another table that is linked

Everywhere I look I can find how to update a table from data in another table but I am not looking for that. I have two tables TABLE1 and TABLE2. TABLE1 has a column PULLDATE and a column JOBNMBR. TABLE2 has a column JOBNMBR and a column PROJECT. The two tables link at the JOBNMBR column. I need to do a bulk update to TABLE1.PULLDATE per a project number, but that project number is stored in TABLE2.PROJECT.
Using VisualStudio 2005 and in VB code not C+, does anyone know the code (if there is any) that links the tables and allows me to update all TABLE1.PULLDATE records grouped by TABLE2.PROJECT? I will be providing the trigger to update using a textbox [TxtBox_Pulldate] and a nearby button [Button_UpdatePulldate].
Thanks a bunch
Chuck Vensel
I think I understand that you want to update Table1 given a matching column in Table2?
You write the SQL update just as you would the SELECT except replace the SELECT clause with the UPDATE clause.
UPDATE Table1
SET
[PULLDATE] = your_value
FROM
Table1
JOIN Table2
ON Table2.[JOBNMBR] = Table1.[JOBNMBR]
WHERE
Table2.[PROJECT] = your_project_ID

Resources