PL/SQL ONLY using a cursor to retrieve data from 2 tables into a new table - oracle

Hi I need to create a new table to store the data of two tables. Then I would need to create a stored procedure to transfer the data from the two tables into the new one. I had no problem transferring the data from the first table to the new table, the problem arises when i transfer the data from the second table to the new table. Because instead of being inserted in the row that matches its pubid, it is being inserted into a new row instead.

You're quite close, I'd say.
In the second loop, you shouldn't INSERT, but UPDATE existing row.
Cursor you declared:
CURSOR cur_proceedings IS
SELECT pubid, year FROM proceedings;
You've used it here:
OPEN cur_proceedings;
LOOP
FETCH cur_proceedings INTO v_proceedings;
EXIT WHEN cur_proceedings%NOTFOUND;
-- Instead of INSERT:
-- INSERT INTO publication_master(proceeding_year)
-- VALUES(v_proceedings.year);
-- use UPDATE:
update publication_master m set
m.proceeding_year = v_proceedings.year
where m.pubid = v_proceedings.pubid;
END LOOP;
CLOSE cur_proceedings;

Related

Oracle SQL / PLSQL : I need to copy data from one database to another

I have two instances of the same database, but data is only committed to the "original" one. I need to copy inserted data from certain tables and commit them to the same tables in the second DB automatically. How can I do it?
I've already created synonyms for the tables in the second DB on original and within a specially prepared trigger I tried to use INSERT INTO ... statement with :new. but it is causing the data to not be committed anywhere and I receive Oracle Errors like:
ORA-02291: integrity constraint (PRDBSHADOW.FK_ED_PHY_ENT) violated.
Here is my trigger code
create or replace TRIGGER INS_COPY_DATA
AFTER INSERT ON ORIGDB.TABLE_A
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
insert into COPY_TABLE_A(val1,val2,val3,val4) values (:new.val1, :new.val2, :new.val3, :new.val4);
END;
I think the entry in parent table is missing here. At least the FK ending of constraint is telling me so.
It means you need to insert first all the data into a "parent" table in order to be able to insert records in a "child".
For example the table auto_maker is having 3 rows only: Audi, Peugeot, and Honda.
Another table named "model" has 2 columns "maker" and "model". "maker" is a foreign key referencing to the "auto_maker" table.
It means in the models table are only the records allowed whose "maker" column value exists in "auto_maker" table.
In other words only these are available:
maker model
Audi A4
Peugeot 308
Honda Accord
Of course you can enter every model you wish, but "maker" value has to exist in the auto_maker table.
This is what probably happen - the trigger tries to insert a data in a column which is referencing to a "parent" table and the :new value just doesn't exist.
The following script will let you know what table you need to fill first.
select aic.index_owner, aic.table_name, aic.column_name
from all_constraints uc,
all_ind_columns aic
where aic.INDEX_NAME = uc.r_constraint_name
and uc.table_name = 'TABLE_A'
and uc.constraint_type = 'R';
If the query returns something just create similar triggers on those tables with similar logic you already have

trigger performance over different cases

I have a table that collects logs of every check_in. I want to pick 4 columns to later process them. I have placed a trigger on every insert and columns are copied. I am confused over which way to do it. For best PERFORMANCE. Below are some different ways I figured out. Assuming my destination table is indexed.
ALTER trigger GetCheckIn_HA_Transit
on tableXyz
AFTER INSERT
as
declare #cardName nvarchar(max)
declare #checkIn datetime
declare #direction varchar(30)
declare #terminal varchar(30)
select
#direction = str_direction,
#terminal = TERMINAL,
#cardName = card_number,
#checkIn = transit_date
from tableXyz
GO
INSERT INTO logs(direction,terminal,cardName,checkIn)
VALUES
(#direction,#terminal,#cardName,#checkIn)
end
Another way i found was without declaration
ALTER trigger GetCheckIn_HA_Transit
on tableXyz
AFTER INSERT
as
GO
INSERT INTO
logs(direction,terminal,cardName,checkIn)
SELECT(STR_DIRECTION,TERMINAL,card_number,transit_date)FROM tableXyz
And is copying data from one table to another table is better in terms of performance than to copy from one database table to another database table over same server ??
Assuming every second we have an insert that will trigger our Trigger.

Archiving data in oracle

i am trying to Archive my data from one table to another.Please find below my requirement.
I have a table A and another table B.
I need to find all the records from A which is less than a particular date
After identifying the records ,i need to move the Records to table B
Once the data is moved to Table B,I need to delete those records from table A.
I am planning to use a Stored procedure with the number of days to archive as parameter.
Now i need to check for the errors while inserting in the table A and should not delete those records in table B and also if the records is successfully inserted in Table A and if fails in the deletion of Table B.Then i need to rollback the record inserted in Table A.
I need to archive on a daily basis and there will at least a million records to archive.
I started with the coding by using the forall and save exceptions but struck with the logic .
Can anyone help me with this logic.
First of all, I'm doubt if such 'archieving' is a good idea. It seems like transferring soup from one plate to another using teaspoon. There are better decisions exist for almost every task, say, using partitioning and maybe exchange partition.
But if you immovably wish to do this, you should write something like this:
procedure Move_Many_Records is
begin
savepoint MMR;
insert /*+ APPEND */ into TARGET (fields)
select fields from SOURCE where {condition};
delete from SOURCE
where id in (select id from TARGET);
savepoint MMR;
exception
when others then
rollback to savepoint MMR;
My_Alerts.Shit_Happens('Failed to move records!');
raise;
end;

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.

Insert data into a different column when copying from one table to another in Oracle

I am using an AFTER INSERT row trigger in Oracle 11g to copy specific columns from one table to another on insert. I have the trigger and insert working ok. The problem I have is that I need to insert the new data from one column to a different column when copying it.
The trigger info reads:
BEGIN
insert into BALES_STORAGE
(CROP,
CUTTING,
DESTINATION,
BALES_MOVED,
DATE_MOVED,
PASTURE,
TARGET_LB_PER_DAY)
values
(:new.CROP,
:new.CUTTING,
:new.MOVING_LOCATION,
:new.BALES_MOVED,
:new.DATE_MOVED,
:new.PASTURE,
:new.TARGET_LB_PER_DAY);
END;
The first table is called "BALES_HARVESTED" and the 2nd table the trigger inserts the selected columns into is called "BALES_STORAGE". I need to insert the :new.MOVING_LOCATION data into the column called DESTINATION on the second table.
So my question is: when using an after insert row trigger, how to I change the column that the data is inserted into?
Thanks for any help.
Matthew
Your trigger code worked just fine for me. Not sure what the problem is. The 3rd column in your INSERT statement does the column mapping correctly.
http://sqlfiddle.com/#!4/2d2fd5/1/1
Maybe you have different structures or foreign key constraints. Could you elaborate on what error you get? Does it produce an ORA- error? or does it simply not produce the desired result, but no error?

Resources