Statement level Trigger or Row Level Trigger - oracle

Which trigger to use - Statement or Row level?
If I use row level trigger , how to restrict it to insert data in tracking table only once for a group of entries in VALUES table
Database Schema -
ITEM_VERSIONS TABLE - Item_id,Version id,start_version_id,end_version_id
ITEM_VALUES_TABLE- Value Id, Value, Item_id, Start_version , End Version
Each time an item is created :
ITEM_VERSIONS TABLE - This table stores all the information of Item Versions.
For 1 item version, it has 1 row.
ITEM_VALUES_TABLE - This has all the values info .
So for 1 item , this table has 15-20 rows for each value attribute. Lets say Item_date , color etc
I want to create a trigger on ITEM_VALUES_TABLE and insert the version id in the tracking table.
To get version id, I have to use ITEM_VERSIONS TABLE to get the version id of the item and then update in the tracking table

Related

Access "Page Items to Submit" values inside a trigger in oracle apex

I want to access extra page item value from a trigger which isn't in the related data table. For an example I have a table like below,
Employee
----------------------
empId | fName | lName
----------------------
1 | John | Doe
----------------------
2 | Jane | Doe
Apex form items will be like,
P500_EMPID, P500_FNAME, P500_LNAME
I have an extra page item called P500_SOMEIDSwhich is a multi select list. I want to access those selected values inside After Insert trigger of the Employee table. I tried to add this item into "Page Items to Submit". But I do not know how to access it inside that trigger. Is it possible..? and How..?
In the page process that handles your table update (that will be a process of type "Form - Automatic Row Processing (DML)", under "Settings" there is a attribute "Return Primary Key(s) after Insert". If that is set to "On", then the insert statement will return the value of the inserted row into the page item that is defined as your primary key.
Example:
Create a form on emp
Make P1_EMPNO the primary key page item
Suppose you create a new row, and that empno value is 1000. Then after the automatic row processing page process is run, the value of P1_EMPNO is set to 1000.
If you want to insert rows into another table referencing the newly created empno then you can create a page process (that executes after the row processing) of type pl/sql code like this:
BEGIN
INSERT INTO some_other_table (empno) VALUES (:P1_EMPNO);
END;
Using triggers for business functionality should be avoided whenever possible.
Instead of a database trigger, use a stored procedure which will do the same job. Pass any number of parameters you want (including the P500_SOMEIDS item).

Disparity between populated id column and returned value from hibernate after save

A sequence is used to generate id for a customer table and then using trigger to populate the id column as a varchar value.
The trigger is
CREATE OR REPLACE TRIGGER USER_ID_TRIGGER
before insert on CUSTOMER
REFERENCING NEW AS New OLD AS Old
for each row
begin
-- :new.user_id := 'CUST' || :new.user_id ;
select 'CUST' || SEQ_CUSTOMER_ID.nextval into :new.user_id from dual;
end USER_ID_TRIGGER;
So, each insert in the column is like 'CUST1', 'CUST3', etc.
The trigger can't be changed.
I am using hibernate save(customer) method to save customer objects in the db. Problem is the return id value I am getting is (as expected) different from the one that is ultimately saved in the table.
For example, if the id populated in the column is 'CUST19', the hibernate code returns 18.
My question is, if the code returns a value of 210, will it be safe for me to assume that the populated value is CUST211?

How to use new ID for subsequent inserts in same transaction

Using Oracle, we have 2 tables - Parent and Child.
We have the ID column as GUID in Parent table and this value is being created by a trigger before insert.
Now while inserting records in same transaction, I need to
- First add a record to Parent table, then
- Use the new GUID created in trigger to add records to child table
How do I retrieve this new GUID for subsequent inserts?
you can use returning into clause of insert statement
INSERT INTO parent VALUES (col1, ...)
RETURNING <your id column > INTO < variable>;
insert into child (parent_id) values (< variable>);

Create after insert trigger which updates record by getting value from other Oracle 11g table

I have two tables
Parent(id, name, occupation)
Child(id, name, gender,parent_id, parent_name, parent_occupation)
Now to insert value in child i'll run chi query
insert into Child(id,name,gender,parent_id) values(10,'XYZ','Male',15);
So now my requirement is when this insert query is executed a trigger will run and get name and occupation from parent table for id 15 (parent_id of the Child record) and add it to the newly inserted row in fields parent_name and parent_occupation respectively.
I am using Oracle 11g as my database.
You want something akin to this (though you'll need to add code to handle the exception):
CREATE OR REPLACE
TRIGGER ai_child_tg
AFTER INSERT ON child
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
SELECT name,
occupation
INTO :NEW.parent_name,
:NEW.parent_occupation
FROM parent
WHERE id = :NEW.parent_id;
EXCEPTION
WHEN no_data_found
THEN
<handle_your_exception_>
END ai_child_tg;
However, if your CHILD table is really a relational child to your PARENT table and there is a FK relationship in place (via the CHILD.PARENT_ID column) then storing the PARENT_NAME and PARENT_OCCUPATION columns in the CHILD table is logically redundant.
I'd query why you have those two columns in the CHILD table at all.
Hope it helps...

Oracle creating a table

How should we create a table which has a column as the average of the totals present in previous columns?
For ex :Departments (depno,depname,noofempl,totalsal,avgsal)
here value in avgsal must be (totalsal/noofempl)
If you are using Oracle 11 you might use virtual columns otherwise you can create a view that selects all your table columns and adds the average column
CREATE TABLE DEPARTMENTS
(
DEPNO...
DEPNAME...
noofempl...
totalsal...
);
CREATE VIEW VW_DEPARMENTS AS
SELECT DEPNO, DEPNAME, noofempl, totalsal, totalsal/noofempl as avgsal
FROM DEPARTMENTS;

Resources