BULK COLLECT in a trigger - sql

I need to create a trigger that updates a column in a table based on a transaction that happens in another table. Is there a way to use BULK COLLECT on joining two tables?
I need to collect data multiple times, is it possible to use BULK COLLECT inside another BULK COLLECT?
HERE is my existing trigger
create or replace trigger trans_hist_trg
AFTER INSERT OR UPDATE OF reason ON transaction_history
FOR EACH ROW
DECLARE
v_exists VARCHAR2(1);
v_valid code.valid_code%TYPE;
v_person_id person.id%TYPE;
TYPE Anumber_Type is TABLE of person.registration_number%TYPE INDEX BY binary_INTEGER;
v_NumberList Anumber_Type;
v_primaryAnumber person.primary_number%TYPE;
v_secondaryAnumber consolidated_numbers.secondary_number%TYPE;
v_anumber person.registration_number%TYPE;
BEGIN
IF(INSERTING) THEN
v_person_id := :NEW.person_id;
ELSE
v_person_id := :OLD.person_id;
END IF;
BEGIN
SELECT p.registration_number, p.primary_number, c.secondary_number INTO v_anumber, v_primaryAnumber, v_secondaryAnumber
FROM person p
LEFT JOIN consolidated_numbers c ON p.id = c.person_id WHERE p.id = v_person_id;
END;
BEGIN
SELECT women_act INTO v_exists
FROM person
WHERE id = v_person_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_exists := NULL;
END;
IF v_exists IS NULL AND :NEW.type_id IN (10,20,30,40,50) THEN
IF :NEW.reason NOT IN ('A1','B1') OR (:NEW.reason IN ('A1','B1') AND :NEW.action_date >= '01-JAN-00') THEN
BEGIN
SELECT valid_code INTO v_valid
FROM code
WHERE valid_code = :NEW.reason;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_exists := null;
END;
IF v_valid IS NOT NULL THEN
SELECT CASE
WHEN EXISTS (SELECT 1 FROM code WHERE valid_code = v_valid)
THEN 'Y' ELSE 'N' END INTO v_exists FROM dual;
END IF;
IF v_exists = 'Y' THEN
select registration_number BULK COLLECT into v_NumberList
FROM person where (registration_number=v_primaryAnumber OR
registration_number=v_anumber OR
registration_number=v_secondaryAnumber OR
primary_number= v_secondaryAnumber OR
primary_number=v_anumber OR
primary_number=v_primaryAnumber ) and (primary_number IS NOT NULL or primary_number <>'000000');
ELSE
select registration_number BULK COLLECT into v_NumberList
FROM person where (registration_number=v_primaryAnumber OR
registration_number=v_anumber OR
registration_number=v_secondaryAnumber OR
primary_number=v_anumber OR
primary_number=v_secondaryAnumber OR
primary_number=v_primaryAnumber OR
primary_number IS NULL);
END IF;
FOR indx IN 1 .. v_NumberList.COUNT
LOOP
update person set women_act = 'X'
where registration_number=v_NumberList(indx) and (women_act<>'X' or women_act IS NULL);
END LOOP;
update person set women_act = 'X'
where registration_number=v_anumber and (women_act<>'X' or women_act IS NULL);
END IF;
END IF;
mmm
END trans_hist_trg;
I need to make this block of code to be my main outer loop to iterate through all the numbers. But I'm unsure how. Please help.
SELECT p.registration_number, p.primary_number, c.secondary_number INTO v_anumber, v_primaryAnumber, v_secondaryAnumber
FROM person p
LEFT JOIN consolidated_numbers c ON p.id = c.person_id WHERE p.id = v_person_id;
Thank you!

Related

fetch multiple rows, store in 1 variable and insert into table

I have to fetch bulk record and insert into table using loop
I have little confusion how to fetch and insert record using loop. Below I have shared what I have done so far .
declare
stud_Id varchar;
begin
stud_Id := select student_id from student_backup where is_active_flg ='Y';
for i in 1 ..stud_Id.count
loop
insert into users(student_id,password,status) values(stud_Id(i),'password','status')
where not exists (select student_id from users where student_id=stud_Id(i))
end loop;
commit;
end;
/
You can use the following :
declare
stud_Id student_backup.student_id%type;
begin
select nvl(max(student_id),0) into stud_Id
from student_backup
where is_active_flg ='Y';
if stud_Id >0 then
for i in 1 ..stud_Id
loop
insert into users(student_id,password,status)
select b.student_id,'password','status'
from student_backup b
left join users u on b.student_id = u.student_id
where is_active_flg ='Y'
and b.student_id = i;
end loop;
end if;
commit;
end;
/
Demo
P.S. If I understood you want to perform, you don't need to use for loop(including if statement) and the select statement in the beginning, but directly apply the insert statement by removing the part and b.student_id = i.
So, convert your block to the one as below :
declare
stud_Id student_backup.student_id%type;
begin
insert into users(student_id,password,status)
select b.student_id,'password','status'
from student_backup b
left join users u on b.student_id = u.student_id
where is_active_flg ='Y' ;
commit;
end;
/
Abdul,
I think you are searching for the following:
BEGIN
INSERT INTO USERS
SELECT STUDENT_ID, PASSWORD , STATUS
FROM student_backup
WHERE STUDENT_ID NOT IN (SELECT STUDENT_ID FROM USERS)
AND is_active_flg = 'Y';
END;
/
Hope, this will be useful.
Demo

I want to create trigger but I dont know how to do that. Below is the sample

I want to create trigger but I don't know how to do that. Below is the sample:
CREATE OR REPLACE TRIGGER checkDuration
after UPDATE on comm_shipping
FOR EACH ROW
DECLARE
quantity NUMBER;
BEGIN
SELECT * FROM comm_orders c
WHERE C.ORDER_ID = :new.order_id
AND c.quantity = 1;
IF quantity=NULL THEN
RAISE_APPLICATION_ERROR('no more items');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
Try the following ( I suppose your DBMS is Oracle or DB2 because of RAISE_APPLICATION_ERROR ):
CREATE OR REPLACE TRIGGER checkDuration
AFTER UPDATE ON comm_shipping
FOR EACH ROW
DECLARE
quantity NUMBER;
BEGIN
SELECT c.quantity
INTO quantity -- this part is missing
FROM comm_orders c
WHERE c.order_id = :new.order_id;
IF quantity is NULL THEN
RAISE_APPLICATION_ERROR(-20001,'no more items'); -- you need a code
-- between -20000 and -20999
END IF;
EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
END;

How do I enter the data in this table in the insert?

I am new in pl / sql , I want to enter this data in the insert someone help me please?
I do not know how to enter this data in the insert without making a join
I can not find the way to enter the data in the insert
these SELECT PORCENTAJE
FROM RANGO_SUBIDA_COMISION;
VARIABLE B_ANIO NUMBER;
EXECUTE :B_ANIO := 042018;
DECLARE
V_EMPLEADO_ID EMPLEADOS.EMPLEADO_ID%TYPE;
V_NOMBRE EMPLEADOS.NOMBRE%TYPE;
V_APELLIDO EMPLEADOS.APELLIDO%TYPE;
V_VALOR_COMISION COMISIONES.VALOR_COMISION%TYPE;
V_SUELDO EMPLEADOS.SUELDO%TYPE;
BEGIN
FOR R IN
(
SELECT DISTINCT E.EMPLEADO_ID , E.NOMBRE , E.APELLIDO , C.VALOR_COMISION , E.SUELDO
FROM EMPLEADOS E JOIN COMISIONES C
ON (C.EMPLEADO_ID = E.EMPLEADO_ID)
)
LOOP
V_EMPLEADO_ID := R.EMPLEADO_ID;
V_NOMBRE := R.NOMBRE;
V_APELLIDO := R.APELLIDO;
V_VALOR_COMISION := R.VALOR_COMISION;
V_SUELDO := R.SUELDO;
INSERT INTO INFORME_SUBIDA_COMISION
VALUES ( :B_ANIO ,V_EMPLEADO_ID , V_NOMBRE , V_APELLIDO , V_VALOR_COMISION , V_SUELDO );
END LOOP;
END;
I'm not quite sure what your question is. But I don't understand why you are using a cursor for this. Why not just execute a query like this?
INSERT INTO INFORME_SUBIDA_COMISION (ANIO, EMPLEADO_ID, NOMBRE, APELLIDO, VALOR_COMISION, SUELDO)
SELECT DISTINCT :B_ANIO, E.EMPLEADO_ID, E.NOMBRE, E.APELLIDO,
C.VALOR_COMISION, E.SUELDO
FROM EMPLEADOS E JOIN COMISIONES C
ON C.EMPLEADO_ID = E.EMPLEADO_ID;
Note: I'm guessing what the column names are in INFORME_SUBIDA_COMISION, but you should explicitly list them.
you can use this insert in Bulk Mode if you have huge data to insert
SET SERVEROUTPUT ON
/*use this script to insert data in bulk mode.*/
DECLARE
TYPE array IS
TABLE OF informe_subida_comision%rowtype;
v_data array;
dml_errors EXCEPTION;
PRAGMA exception_init ( dml_errors,-24381 );
CURSOR l_cur IS
SELECT DISTINCT
e.empleado_id,
e.nombre,
e.apellido,
c.valor_comision,
e.sueldo
FROM
empleados e
JOIN comisiones c ON (
c.empleado_id = e.empleado_id
);
v_error_count NUMBER;
v_err VARCHAR2(500);
v_err_indx NUMBER;
BEGIN
OPEN l_cur;
LOOP
BEGIN
FETCH l_cur BULK COLLECT INTO v_data LIMIT 1000;
FORALL i IN 1..v_data.count SAVE EXCEPTIONS
--insert data into the table
INSERT INTO informe_subida_comision VALUES v_data ( i );
-- log any dml errors
EXCEPTION
WHEN dml_errors THEN
v_error_count := SQL%bulk_exceptions.count;
FOR i IN 1..v_error_count LOOP
v_err_indx := SQL%bulk_exceptions(i).error_index;
v_err := sqlerrm(-SQL%bulk_exceptions(i).error_code);
dbms_output.put_line(v_data(v_err_indx).empleado_id
|| ''
|| v_err);
END LOOP;
END;
COMMIT;
EXIT WHEN l_cur%notfound;
END LOOP;
CLOSE l_cur;
END;

INSERT and UPDATE a record using cursors in oracle

I have 2 tables- student and studLoad both having 2 fields studID and studName. I want to load data from student table into stuLoad table.
If the data already exists in the studLoad table, then it should be updated else it should be inserted. following is my code to do so:
create or replace procedure studentLoad is
v_id student.studID%type;
v_name student.studName%type;
v_sn studLoad.studName%type;
cursor cur_load is
select * from student;
begin
open cur_load;
loop
fetch cur_load into v_id,v_name;
exit when cur_load%notfound;
select studName into v_sn from studLoad where studID = v_id;
if(v_sn!= v_name) then
update studLoad set studName= v_name where studID= v_id;
else
insert into studLoad values(v_id,v_name);
dbms_output.put_line(v_id || ' ' || v_name);
end if;
end loop;
close cur_load;
end;
It's not working. the rows in studLoad table are noT updated. How do I solve this? In SQL server we use IF EXISTS(select...from stuLoad..) to check if the record exists in the table, is there a way to do the same in Oracle? if yes then please let me know the same.
This is a highly inefficient way of doing it. You can use the merge statement and then there's no need for cursors, looping or (if you can do without) PL/SQL.
MERGE INTO studLoad l
USING ( SELECT studId, studName FROM student ) s
ON (l.studId = s.studId)
WHEN MATCHED THEN
UPDATE SET l.studName = s.studName
WHERE l.studName != s.studName
WHEN NOT MATCHED THEN
INSERT (l.studID, l.studName)
VALUES (s.studId, s.studName)
Make sure you commit, once completed, in order to be able to see this in the database.
To actually answer your question I would do it something like as follows. This has the benefit of doing most of the work in SQL and only updating based on the rowid, a unique address in the table.
It declares a type, which you place the data within in bulk, 10,000 rows at a time. Then processes these rows individually.
However, as I say this will not be as efficient as merge.
declare
cursor c_data is
select b.rowid as rid, a.studId, a.studName
from student a
left outer join studLoad b
on a.studId = b.studId
and a.studName <> b.studName
;
type t__data is table of c_data%rowtype index by binary_integer;
t_data t__data;
begin
open c_data;
loop
fetch c_data bulk collect into t_data limit 10000;
exit when t_data.count = 0;
for idx in t_data.first .. t_data.last loop
if t_data(idx).rid is null then
insert into studLoad (studId, studName)
values (t_data(idx).studId, t_data(idx).studName);
else
update studLoad
set studName = t_data(idx).studName
where rowid = t_data(idx).rid
;
end if;
end loop;
end loop;
close c_data;
end;
/
If you would like to use your procedure, consider to change some lines:
create or replace procedure studentLoad is
v_id student.studID%type;
v_name student.studName%type;
v_sn studLoad.studName%type;
cursor cur_load is
select * from student;
begin
open cur_load;
loop
fetch cur_load into v_id,v_name;
exit when cur_load%notfound;
begin
select studName into v_sn from studLoad where studID = v_id;
if(v_sn!= v_name) then
update studLoad set studName= v_name where studID= v_id;
end if;
exception
when no_data_found then
insert into studLoad values(v_id,v_name);
end;
dbms_output.put_line(v_id || ' ' || v_name);
end loop;
close cur_load;
end;
I think it should work, didn't test it.

Getting an error in sql, when executing code below.How to declare a table type in plsql. Am a beginner . Please suggest

create or replace procedure BAS_NUM_UPD is
cursor cur is
select distinct o.oi_b,mpr.pa_ke_i,ltrim(substr(convert_171_to_711(cp.p_t_num),1,7),'0') bs_nbr
from t_obj o, mat_pa_rel mp, cor_pa cp
where o.ob_t = 'something'
and o.oi_b = mp.oi_b
and mp.pa_ke_i = cp.pa_ke_i;
l_ba_num_at_i number(10) := get_attribute_id('Ba timber');
flag1 VARCHAR2(10);
type t1 is table of varchar2(10);
par_k t1;
BEGIN
for x in cur loop
BEGIN
select pa_ke_i into par_k from mat_pa_rel where oi_b=x.oi_b ;
if par_k.count=null then
insert into cs_val (oi_b, at_i, value, flag, ) values (x.oi_b, l_ba_num_at_i, null, 1);
end if;
select flag into flag1 from cs_val where at_i = l_ba_num_at_i and oi_b = x.oi_b
and value = x.bs_nbr;
EXCEPTION
when NO_DATA_FOUND THEN
insert into cs_val (oi_b, at_i, value, flag, )
values (x.oi_b, l_ba_num_at_i, x.bs_nbr, 1);
flag1 :='Nothing';
when OTHERS
then
raise_application_error(-20011,'Unknown Exception in PROCEDURE');
END;
end loop;
end BAS_NUM_UPD;
error:
PLS-00642: local collection types not allowed in SQL statements
You should get it running if you do a bulk collect
select pa_ke_i bulk collect into par_k from mat_pa_rel where oi_b=x.oi_b ;
Then I think the if is not right. I think you need to do
if par_k.count = 0 then
But to be honest you might just make a count
select count(*) into l_cnt from mat_pa_rel where oi_b=x.oi_b;
If l_cnt = 0 then ...
Of course l_cnt has to be defined.
You should create type t1 in the schema and not in the pl/sql block.