I'm trying to create a trigger and I get the following error:
Error(24,5): PLS-00103: Found the symbol "BEGIN" when it was expected one of the following: * & - + / at loop mod remainder rem and or || multiset. I'm quite a newbie, thanks in advance!
CREATE OR replace TRIGGER ins_livro
instead OF INSERT ON viewLivros
FOR EACH ROW
DECLARE
cnt NUMBER := 10;
biggestID Number;
BEGIN
Select max(exemplar_id) into biggestID from exemplar;
INSERT INTO livro (
id_livro,
nome_livro,
id_editora,
ano,
Preco_Aluguer,
Preco_Compra,
Preco_Multa
)
VALUES (:new.id_livro,
:new.nome_livro,
:new.id_editora,
:new.ano,
:new.Preco_Aluguer,
:new.Preco_Compra,
:new.Preco_Multa
);
WHILE cnt > 0
BEGIN
SET biggestID = biggestID + 1
INSERT INTO exemplar (
id_exemplar,
id_livro
)
VALUES (
:new.biggestID,
:new.id_livro
);
SET cnt = cnt - 1
END;
END;
You are missing the loop and end loop clauses:
WHILE cnt > 0
LOOP
BEGIN
SET biggestID = biggestID + 1
INSERT INTO exemplar (
id_exemplar,
id_livro
)
VALUES (
:new.biggestID,
:new.id_livro
);
SET cnt = cnt - 1
END;
END LOOP;
You have a few mistakes in your syntax. Here it is corrected:
CREATE OR REPLACE TRIGGER ins_livro INSTEAD OF
INSERT ON viewLivros FOR EACH ROW DECLARE cnt NUMBER := 10;
biggestID NUMBER;
BEGIN
SELECT MAX(exemplar_id) INTO biggestID FROM exemplar;
INSERT
INTO livro
(
id_livro,
nome_livro,
id_editora,
ano,
Preco_Aluguer,
Preco_Compra,
Preco_Multa
)
VALUES
(
:new.id_livro,
:new.nome_livro,
:new.id_editora,
:new.ano,
:new.Preco_Aluguer,
:new.Preco_Compra,
:new.Preco_Multa
);
WHILE cnt > 0
LOOP
BEGIN
biggestID := biggestID + 1;
INSERT
INTO exemplar
(
id_exemplar,
id_livro
)
VALUES
(
biggestID,
:new.id_livro
);
cnt := cnt - 1;
END;
END LOOP;
END;
Issues:
You can't use this syntax: SET biggestID = biggestID + 1
You need to use: biggestID := biggestID + 1;
Notice the SET keyword has been removed, = has been changed to := and the line has been terminated with a semicolon.
Also, there's no such thing as :new.biggestID. That is a variable that you've defined in the trigger. It needed to be replaced by just biggestID.
Finally, the BEGIN and END around this block were replaced with LOOP and END LOOP:
SET biggestID = biggestID + 1
INSERT INTO exemplar (
id_exemplar,
id_livro
)
VALUES (
:new.biggestID,
:new.id_livro
);
SET cnt = cnt - 1
Related
I am creating a function in Oracle but it's not getting compiled and I am getting two errors
Error 1.
Error: PL/SQL: Compilation unit analysis terminated
Error 2.
Error(2,16): PLS-00201: identifier 'DIV_DUR_PRICE_TABLE' must be
declared
HERE IS THE CODE
CREATE OR REPLACE FUNCTION DIV_DAR(FID IN VARCHAR, DATE1 IN DATE, DATE2 IN DATE)
RETURN DIV_DUR_PRICE_TABLE PARALLEL_ENABLE AS
PRAGMA AUTONOMOUS_TRANSACTION;
CNT NUMBER;
V_RET DIV_DUR_PRICE_TABLE;
BEGIN;
EXECUTE IMMEDIATE 'DELETE from GTT_DIV_DUR_PRICE_TABLE';
Insert Into GTT_DIV_DUR_PRICE_TABLE
(select mydate, column1, column2, 0 as Final_value from tablename)
-- please refer my previous question to understand the code written ahead
declare
v_num integer := 1;
v_column1 number(8,2);
v_column2 number(8,2);
v_Final_value number(8,2);
begin
for rec in (select * from GTT_DIV_DUR_PRICE_TABLE order by mydate)
loop
if(v_num = 1) then
update tab set Final_value = column1 where mydate = rec.mydate;
else
if(rec.column2 is not null) then
update tab set Final_value =
v_Final_value * (v_column1/rec.column1) +
rec.column2 * (v_column1/v_Final_value) where mydate = rec.mydate;
else
update tab set Final_value =
v_Final_value * (rec.column1 / v_column1) where mydate = rec.mydate;
end if;
end if;
v_num:= v_num +1;
v_column1 := rec.column1;
v_column2 := rec.column2;
select final_value into v_Final_value from GTT_DIV_DUR_PRICE_TABLE
where mydate = rec.mydate;
end loop;
end;
SELECT
CAST(
MULTISET(
SELECT * FROM GTT_DIV_DUR_PRICE_TABLE order by P_DATE desc
)AS DIV_DUR_PRICE_TABLE
) INTO V_RET FROM DUAL;
COMMIT;
RETURN V_RET;
END DIV_DAR;
Here , I want to assign all the value of router_index in the same block with the first row of index value in this block.
E.g. The router_index value from Row 1 to 4 should be 5,383 and from 5 to 7 should be 2,703...
I wonder if I can do it with pure PostgreSQL ?
Thanks for your help!!!!
do $$
declare
cnt integer := 0;
head_index_id varchar := '0';
cur_index_id varchar := '0';
fixed_index varchar := '10';
cnt_limit integer;
begin
select max(per_id) from detailed_router into cnt_limit;
while cnt <= cnt_limit loop
select router_index from detailed_router where per_id = cnt into cur_index_id;
if head_index_id = '0' and cur_index_id != fixed_index then
head_index_id := cur_index_id;
else
if cur_index_id = fixed_index then
-- update
update detailed_router set router_index = head_index_id where per_id = cnt;
else
select router_index from detailed_router where per_id = cnt into head_index_id;
end if;
end if;
raise notice 'cnt %', cnt;
raise notice 'cur_index_id %', cur_index_id;
raise notice 'head_index_id %', head_index_id;
cnt := cnt + 1;
end loop;
end $$
If you want "10" values to be filled with the previous non-"10" value and you have a column that specifies the ordering, you can use window functions:
select t.*,
max(router_index) over (partition by driver_index_code, grp) as imputed_router_index
from (select t.*,
count(*) filter (where router_index > 10) over (partition by driver_index_code order by <ordering column>) as grp
from t
) t;
I wrote a procedure where the data gets updated/inserted simultaneously to the destination table from source table. The procedure is working fine for less no of records, but when i try to execute more records its taking more time to perform the operation.
Can we convert merge clause with bulk collect where the logic remains same ? i dint find any useful resources.
I have attached my merge procedure .
create or replace PROCEDURE TEST1 (
p_array_size IN NUMBER
) IS
CURSOR dtls IS SELECT DISTINCT
account_num
FROM
table1
WHERE
rprtd_till_dt = (
SELECT
dt - 1
FROM
dates
WHERE
id = 'odc'
);
TYPE data_tbl IS TABLE OF dtls%rowtype;
data data_tbl;
BEGIN
DECLARE
v_noofDays NUMBER:=0;
currentDt DATE;
BEGIN
SELECT dt INTO currentDt FROM dates WHERE id = 'odc';
BEGIN
OPEN dtls;
LOOP
FETCH dtls BULK COLLECT INTO data LIMIT p_array_size;
EXIT WHEN data.COUNT = 0;
FOR i IN 1..data.COUNT
LOOP
IF(TRUNC(data(i).creation_dt,'MM') = TRUNC(currentDt,'MM')) THEN
v_noofDays := currentDt - 1 - data(i).creation_dt;
ELSE
v_noofDays := currentDt - TRUNC(currentDt,'MM');
END IF;
MERGE INTO table1 updtbl USING ( SELECT
d.*
FROM
table2 d,
(
SELECT
b.prdct_id,
FROM
table3 a,
table2 b
WHERE
a.ir_id = b.ir_id
AND a.price_component_id = b.price_component_id
AND a.financial_institution_id = b.financial_institution_id
GROUP BY
b.prdct_id,
) e
WHERE
d.prdct_id = e.prdct_id
AND d.bndng_typ = data(i).bndng_typ
AND d.bndng_val = data(i).bndng_val
AND d.financial_institution_id = data(i).financial_institution_id
AND d.prdct_id = data(i).prdct_id
AND d.prdct_sub_id = data(i).prdct_sub_id
AND d.instrmnt_id = data(i).instrmnt_id
)
inp ON (
updtbl.POS_NUM = data(i).POS_NUM
AND updtbl.POS_TYPE = data(i).POS_TYPE
AND updtbl.PRICE_COMPONENT_ID = inp.PRICE_COMPONENT_ID
AND updtbl.RPRTD_TILL_DT = data(i).RPRTD_TILL_DT
)
WHEN NOT MATCHED THEN
INSERT VALUES (
data(i).loan_account_num,
inp.ir_id,
inp.price_component_id,
)
WHEN MATCHED THEN
update SET SEQ_NUM=1,
NET_INTRST_AMT=round(data(i).curr_loan_bal*inp.price_component_value*v_noofDays/36000,2),
DM_BTID=200
WHERE SEQ_NUM=2;
COMMIT;
END LOOP;
END LOOP;
CLOSE dtls;
END;
END;
END TEST1;
/
If anyone can help me to guide the syntax on how to achieve the above procedure using bulk collect will be helpful.
I know its a bit late, but use the following for future if you haven't solved it yet
drop table projects;
create table projects (
proj_id integer not null primary key,
proj_title varchar2(20)
);
insert into projects (proj_id, proj_title) values (1, 'Project One');
insert into projects (proj_id, proj_title) values (2, 'Project Two');
commit;
select *
from projects;
declare
type varray_t is varray(2) of projects%rowtype;
arr varray_t;
begin
with test_data as (select 2 as proj_id, 'New Project Two' as proj_title from dual
union all select 3 as proj_id, 'New Project Three' as proj_title from dual)
select proj_id, proj_title
bulk collect into arr
from test_data;
forall i in arr.first .. arr.last
merge into projects
using (select arr(i).proj_id as proj_id,
arr(i).proj_title as proj_title
from dual) mrg
on (projects.proj_id = mrg.proj_id)
when matched then update set projects.proj_title = mrg.proj_title
when not matched then insert (proj_id, proj_title) values (mrg.proj_id, mrg.proj_title);
dbms_output.put_line(sql%rowcount || ' rows merged');
commit;
end;
I hope this will give you kind of idea. Avoid the copy and paste and check the syntax.
create or replace PROCEDURE TEST1 (
p_array_size IN NUMBER
) IS
CURSOR dtls IS SELECT DISTINCT
account_num
FROM
table1
WHERE
rprtd_till_dt = (
SELECT
dt - 1
FROM
dates
WHERE
id = 'odc'
);
TYPE data_tbl IS TABLE OF dtls%rowtype;
data data_tbl;
BEGIN
DECLARE
v_noofDays NUMBER:=0;
currentDt DATE;
BEGIN
SELECT dt INTO currentDt FROM dates WHERE id = 'odc';
BEGIN
OPEN dtls;
LOOP
FETCH dtls BULK COLLECT INTO data LIMIT p_array_size;
EXIT WHEN data.COUNT = 0;
FORALL rec in data.first .. data.last
MERGE INTO table1 updtbl USING (
SELECT
d.* FROM
table2 d,(
SELECT
b.prdct_id
FROM
table3 a,
table2 b
WHERE
a.ir_id = b.ir_id
AND a.price_component_id = b.price_component_id
AND a.financial_institution_id = b.financial_institution_id
GROUP BY
b.prdct_id
) e
WHERE
d.prdct_id = e.prdct_id
AND d.bndng_typ = data(rec).bndng_typ
AND d.bndng_val = data(rec).bndng_val
AND d.financial_institution_id = data(rec).financial_institution_id
AND d.prdct_id = data(rec).prdct_id
AND d.prdct_sub_id = data(rec).prdct_sub_id
AND d.instrmnt_id = data(rec).instrmnt_id
)
inp ON (
updtbl.POS_NUM = data(rec).POS_NUM
AND updtbl.POS_TYPE = data(rec).POS_TYPE
AND updtbl.PRICE_COMPONENT_ID = data(rec).PRICE_COMPONENT_ID
AND updtbl.RPRTD_TILL_DT = data(rec).RPRTD_TILL_DT
)
WHEN NOT MATCHED THEN
INSERT VALUES (
data(rec)
)
WHEN MATCHED THEN
update SET SEQ_NUM=1,
NET_INTRST_AMT=round(data(rec).curr_loan_bal*inp.price_component_value*v_noofDays/36000,2),
DM_BTID=200
WHERE SEQ_NUM=2;
END LOOP;
CLOSE dtls;
END;
END;
END TEST1;
Merge is always better than forall for atomic updates.
A simplistic use case is
https://ograycoding.wordpress.com/2012/10/13/oracle-merge-v-bulk-collect-and-forall/
There's quite big table, more than 10 000 000 rows. It has columns OBJ_ID, DATE_OF_CHANGE, USER. And I added a new column, RECORD_ID, it is empty for now.
I need to update it so RECORD_ID should have numeric values ascending for OBJ_ID and DATE_OF_CHANGE.
I came up with this:
CREATE SEQUENCE REC_ID_SEQ
START WITH 1
INCREMENT BY 1
CACHE 100;
/
CREATE OR REPLACE TRIGGER TRG_REC_ID_SEQ
BEFORE INSERT ON T_HISTORY
FOR EACH ROW
BEGIN
:NEW.RECORD_ID := REC_ID_SEQ.NEXTVAL;
END;
/
DECLARE
O_ID NUMBER := 0;
S_DATE DATE := SYSDATE;
HIST_NUM NUMBER := 0;
LOOP_COUNT NUMBER := 0;
BEGIN
FOR O IN (SELECT ROWID ROW_ID, D.* FROM T_HISTORY D ORDER BY D.OBJ_ID, D.DATE_OF_CHANGE)
LOOP
LOOP_COUNT := LOOP_COUNT + 1;
IF O.OBJ_ID != O_ID OR O.DATE_OF_CHANGE!= S_DATE
THEN
HIST_NUM := HIST_NUM + 1;
END IF;
UPDATE T_HISTORY T SET T.RECORD_ID = HIST_NUM WHERE T.ROWID = O.ROW_ID;
O_ID := O.OBJ_ID;
S_DATE := O.DATE_OF_CHANGE;
IF LOOP_COUNT > 100000 THEN
COMMIT; LOOP_COUNT := 0;
END IF;
END LOOP;
END;
/
But when the command stops working (no errors) I see that about half of rows were not updated. How do I do this the right way?
Use MERGE command and rowid pseudocolumn as a substitute of primary key:
merge into T_HISTORY t
using (
select rownum as xx, t.*
from (
select t.*, rowid as x_rowid
from T_HISTORY t
order by OBJ_ID, DATE_OF_CHANGE
) t
) xx
on (xx.x_rowid = t.rowid )
when matched then update
set t.RECORD_ID = xx;
Live demo: http://sqlfiddle.com/#!4/aad05/2
Similar to #krokodilko's solution, using analytical function:
MERGE INTO t_history t
USING (SELECT obj_id,
date_of_change,
ROW_NUMBER () OVER (ORDER BY obj_id, date_of_change) rn
FROM t_history) r
ON (t.obj_id = r.obj_id AND t.date_of_change = r.date_of_change)
WHEN MATCHED
THEN
UPDATE SET t.record_id = r.rn;
I'm wirting and Pl_SQL Script to do some inserts if an condition not Exist
and its not working for me :(
this is my code
paramS in an CURSOR
FOR ps IN paramS LOOP
compteur := 5;
LOOP
IF NOT EXISTS
(SELECT *
FROM carp.Table1
WHERE FK_tab1 =ps.id_tab1
AND DIC_TYPE = compteur
)
BEGIN
INSERT
INTO carp.Table1
(
id,
FK_tab1,
DIC_TYPE
)
VALUES
(
id.nextval ,
ps.id_tab1 ,
compteur
)
END
compteur:=compteur-1;
commit;
EXIT WHEN compteur <0;
END LOOP;
END LOOP;
thanks in advance
Usually I use such checking , If not exists doesnt work on oracle (as far as I know)
DECLARE
CNT NUMBER(4);
BEGIN
SELECT COUNT(1) INTO CNT
FROM carp.Table1
WHERE FK_tab1 =ps.id_tab1
AND DIC_TYPE = compteur
IF CNT = 0 THEN
INSERT
INTO carp.Table1
(
id,
FK_tab1,
DIC_TYPE
)
VALUES
(
id.nextval ,
ps.id_tab1 ,
compteur
)
End IF;
END;
/
commit
/
You can use NO_DATA_FOUND exception
BEGIN
SELECT * FROM carp.Table1
WHERE FK_tab1 = ps.id_tab1
AND DIC_TYPE = compteur
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO carp.Table1 (id, FK_tab1, DIC_TYPE)
VALUES(id.nextval, ps.id_tab1, compteur);
COMMIT;
END
You can avoid count() using:
DECLARE FG_EXISTS NUMBER(1);
BEGIN
SELECT CASE WHEN EXISTS(SELECT 1
FROM CARP.TABLE1
WHERE FK_TAB1 =PS.ID_TAB1
AND DIC_TYPE = COMPTEUR
) THEN 1 ELSE 0 END INTO FG_EXISTS
FROM DUAL;
IF FG_EXISTS = 0 THEN INSERT ...