I am trying to run while loop in snowflake, but I keep getting error
I need to loop through employee and store the result into temp table tblChildren
Appreciate your help in advance!
: syntax error line 7 at position 17 unexpected '<'.
Here is the code:
set ro = (select count(*) from tblEmployees);
execute immediate $$
begin
set counter := 1;
set iRows = ro;
set EmployeeID = '';
while(counter < iRows) do
select EmployeeID = TblEmployeeID from tblEmployees emp where emp.RowId = i
insert into tblChildren
with cteChildren as
(
select emp.tblEmployeeID as ChildId, emp.WorkEmail, emp.EmployeeManagerId
from tblEmployees emp
where emp.EmployeeManagerId = EmployeeID
union all
select emp.tblEmployeeID as ChildId, emp.WorkEmail, emp.EmployeeManagerId
from tblEmployees emp
join cteChildren on emp.EmployeeManagerId = cteChildren.ChildId
)
select EmployeeID as EmployeeManagerId, c.ChildId from cteChildren c
select i = i + 1
end while;
end;
$$;
There are lots of errors in your script. I tried to fix them, and this one work but not sure if it works as you expected:
set ro = (select count(*) from tblEmployees);
execute immediate $$
declare
counter number;
iRows number;
EmployeeID varchar;
begin
counter := 1;
iRows := $ro;
EmployeeID := '';
while(counter < iRows) do
select TblEmployeeID into :EmployeeID from tblEmployees emp where emp.RowId = :counter;
insert into tblChildren
with cteChildren as
(
select emp.tblEmployeeID as ChildId, emp.WorkEmail, emp.EmployeeManagerId
from tblEmployees emp
where emp.EmployeeManagerId = :EmployeeID
union all
select emp.tblEmployeeID as ChildId, emp.WorkEmail, emp.EmployeeManagerId
from tblEmployees emp
join cteChildren on emp.EmployeeManagerId = cteChildren.ChildId
)
select :EmployeeID as EmployeeManagerId, c.ChildId from cteChildren c;
counter := counter + 1;
end while;
end;
$$;
Related
I want to get all products inside an order. I give to my procedure the ID of the order and then I want to list all of them.
Here is the database scheme of the tables I use in this procedure:
And here is the procedure:
CREATE OR REPLACE PROCEDURE exercitiu6(v_ID_Comanda Comanda.ID_Comanda%TYPE) AS
TYPE produse IS TABLE OF Produs%ROWTYPE INDEX BY PLS_INTEGER;
p produse;
TYPE imbricat IS TABLE OF ProduseComanda.ID_Produs%TYPE;
imbricat_produse imbricat:= imbricat();
prod Produs%ROWTYPE;
i number:=0;
j number:=0;
BEGIN
SELECT ID_Produs BULK COLLECT INTO imbricat_produse FROM ProduseComanda
WHERE ID_Comanda = v_ID_Comanda;
FOR i IN imbricat_produse.FIRST..imbricat_produse.LAST LOOP
SELECT ID_Produs, nume, pret INTO prod FROM Produs
WHERE ID_Produs = imbricat_produse(i);
p(j):= prod;
j:= j + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Comanda cu ID-ul ' || v_ID_Comanda || ' contine urmatoarele produse: ');
FOR j IN p.FIRST..p.LAST LOOP
DBMS_OUTPUT.PUT_LINE(p(j).nume);
END LOOP;
END;
I get the Error Sql Statement ignored; too many values on this line:
WHERE ID_Comanda = v_ID_Comanda;
How do I solve this error?
Use a JOIN and a single cursor:
CREATE OR REPLACE PROCEDURE exercitiu6(
v_ID_Comanda ProduseComanda.ID_Comanda%TYPE
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Comanda cu ID-ul ' || v_ID_Comanda || ' contine urmatoarele produse: ');
FOR i IN (
SELECT p.nume
FROM ProduseComanda c
INNER JOIN Produs p
ON p.ID_Produs = c.ID_Produs
WHERE c.ID_Comanda = v_ID_Comanda
)
LOOP
DBMS_OUTPUT.PUT_LINE(i.nume);
END LOOP;
END;
/
Then, for the sample data:
CREATE TABLE produsecomanda (ID_Produs, ID_Comanda) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL;
CREATE TABLE produs (ID_Produs, nume) AS
SELECT 1, 'Alice' FROM DUAL UNION ALL
SELECT 2, 'Beryl' FROM DUAL UNION ALL
SELECT 3, 'Carol' FROM DUAL;
Then:
BEGIN
DBMS_OUTPUT.ENABLE();
exercitiu6(1);
END;
/
Outputs:
Comanda cu ID-ul 1 contine urmatoarele produse:
Alice
Beryl
Carol
If you want to fix your code then the error is not with the WHERE clause but with the mismatch between the number of columns in the SELECT clause and the INTO clause. To fix it you need to use SELECT * INTO ... rather than naming all the columns when you are working with %ROWTYPE variables:
CREATE OR REPLACE PROCEDURE exercitiu6(
v_ID_Comanda ProduseComanda.ID_Comanda%TYPE
)
AS
TYPE produse IS TABLE OF Produs%ROWTYPE INDEX BY PLS_INTEGER;
p produse;
TYPE imbricat IS TABLE OF ProduseComanda.ID_Produs%TYPE;
imbricat_produse imbricat;
i number:=0;
j number:=0;
BEGIN
SELECT ID_Produs
BULK COLLECT INTO imbricat_produse
FROM ProduseComanda
WHERE ID_Comanda = v_ID_Comanda;
FOR i IN imbricat_produse.FIRST..imbricat_produse.LAST LOOP
j:= j + 1;
SELECT *
INTO p(j)
FROM Produs
WHERE ID_Produs = imbricat_produse(i);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Comanda cu ID-ul ' || v_ID_Comanda || ' contine urmatoarele produse: ');
FOR j IN p.FIRST..p.LAST LOOP
DBMS_OUTPUT.PUT_LINE(p(j).nume);
END LOOP;
END;
/
fiddle
I have a store procedure that will
Update maximum 500 rows status from 0 to 1
Return those rows to program via cursor
Here is my store procedure code
PROCEDURE process_data_out (
o_rt_cursor OUT SYS_REFCURSOR
) IS
v_limit NUMBER;
l_data_ids VARCHAR2(32000);
BEGIN
v_limit := 500; -- limit 500
l_data_ids := '';
-- Create loop to get data
FOR i IN (
SELECT *
FROM
(
SELECT id FROM
TBL_DATA a
WHERE
a.created_at BETWEEN SYSDATE - 0.5 AND SYSDATE + 0.1
AND a.status = 0
AND a.phone NOT IN (SELECT phone FROM TBL_BIG_TABLE_1)
AND a.phone NOT IN (SELECT phone FROM TBL_BIG_TABLE_2 WHERE IS_DENY = 1)
ORDER BY
priority
)
WHERE
ROWNUM <= v_limit
) LOOP
BEGIN
-- Build string of ids like id1,id2,id3,
l_data_ids := l_data_ids
|| i.id
|| ',';
-- update row status to prevent future repeat
UPDATE TBL_DATA
SET
status = 1
WHERE
id = i.id;
END;
END LOOP;
COMMIT;
-- If string of ids length >0 open cursor to take data
IF ( length(l_data_ids) > 0 )
THEN
-- Cut last comma id1,id2,id3, --> id1,id2,id3
l_data_ids := substr(l_data_ids,1,length(l_data_ids) - 1);
-- open cursor
OPEN o_rt_cursor FOR
SELECT
id,
phone
FROM
TBL_DATA a
WHERE
a.id IN (
SELECT
to_number(column_value)
FROM
XMLTABLE ( l_data_ids )
);
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END process_data_out;
I want to optimize this performance and here is my question
Should I replace in by exists
Replace
AND a.phone NOT IN (SELECT phone FROM TBL_BIG_TABLE_1)
AND a.phone NOT IN (SELECT phone FROM TBL_BIG_TABLE_2 WHERE IS_DENY = 1)
by
AND NOT Exists (SELECT phone FROM TBL_BIG_TABLE_1 where TBL_BIG_TABLE_1.phone = a.phone)
AND NOT Exists (SELECT phone FROM TBL_BIG_TABLE_2 WHERE TBL_BIG_TABLE_2.phone = a.phone and IS_DENY = 1)
Is there a better way than
Save a string of ids like id1,id2,id3 after update row status
Open cursor by select from string of ids
I appreciate for any suggestion.
Thank for your concern
Row-by-row processing is always slower and you are also creating the string of ids, which again takes time so overall performance is going down.
You can use the collection DBMS_SQL.NUMBER_TABLE to store the updated ids from the UPDATE statement using the RETURNING clause and use it in the cursor query.
Also, I have changed your update statement so that it does not use NOT IN and uses the LEFT JOINS and ROW_NUMBER analytical function for increasing the performance as follows:
CREATE OR REPLACE PROCEDURE PROCESS_DATA_OUT (
O_RT_CURSOR OUT SYS_REFCURSOR
) IS
V_LIMIT NUMBER;
L_DATA_IDS DBMS_SQL.NUMBER_TABLE;
BEGIN
V_LIMIT := 500; -- limit 500
UPDATE TBL_DATA A
SET A.STATUS = 1
WHERE A.ID IN (
SELECT ID
FROM ( SELECT ID,
ROW_NUMBER() OVER(ORDER BY PRIORITY) AS RN
FROM TBL_DATA B
LEFT JOIN TBL_BIG_TABLE_1 T1 ON T1.PHONE = B.PHONE
LEFT JOIN TBL_BIG_TABLE_2 T2 ON T2.IS_DENY = 1 AND T2.PHONE = B.PHONE
WHERE B.CREATED_AT BETWEEN SYSDATE - 0.5 AND SYSDATE + 0.1
AND B.STATUS = 0
AND T1.PHONE IS NULL
AND T2.PHONE IS NULL)
WHERE RN <= V_LIMIT ) RETURNING ID BULK COLLECT INTO L_DATA_IDS;
OPEN O_RT_CURSOR FOR SELECT ID, PHONE
FROM TBL_DATA A
WHERE A.ID IN (SELECT COLUMN_VALUE FROM TABLE ( L_DATA_IDS ));
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END PROCESS_DATA_OUT;
/
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/
i need help with the code, i have two pieces of code that write two variables: v_responsible_job and v_responsible.I would like to combine these queries into one and add a condition if.
If the answer to this query is "ABC", then the first query must be completed, and if not,the second query:
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=4ce28fad7e33f00c5538e49943ae7dechere is the demo data
SELECT NAME FROM data_separators WHERE id = way.DS_ID
example answer:
NAME
ABC Info
Scool
offise
ABC SHOP
first call :
BEGIN
SELECT full_name, job INTO v_responsible, job_id
FROM physical_persons
WHERE id IN (SELECT physical_person
FROM data_separators WHERE id = way.DS_ID) AND rownum = 1;
SELECT name
INTO v_responsible_job
FROM jobs
WHERE id = job_id AND
rownum = 1;
if v_responsible_job is not null then
if length(v_responsible_job) > 0 then
v_responsible_job := ', '|| v_responsible_job;
end if;
end if;
EXCEPTION WHEN OTHERS THEN
v_responsible_job := '';
END;
second call :
BEGIN
SELECT full_name, job
INTO v_responsible, job_id
FROM physical_persons
WHERE id IN (SELECT RESPONSIBLE
FROM ADRESSES
WHERE name = p1.name) AND
rownum = 1;
SELECT name
INTO v_responsible_job
FROM jobs
WHERE id = job_id AND
rownum = 1;
if v_responsible_job is not null then
if length(v_responsible_job) > 0 then
v_responsible_job := ', '|| v_responsible_job;
end if;
end if;
EXCEPTION WHEN OTHERS THEN
v_responsible_job := '';
END;
my varianr answer , but his don't work(
BEGIN
select count(*) into v_count_word FROM data_separators WHERE id = way.DS_ID and NAME LIKE '%ABC%';
select count(RESPONSIBLE) into v_count_respon FROM ADRESSES WHERE name = p1.name;
if v_count_word > 0 then
SELECT full_name, job INTO v_responsible, job_id FROM physical_persons WHERE id IN (SELECT physical_person FROM data_separators WHERE id = way.DS_ID) AND rownum = 1;
SELECT name INTO v_responsible_job FROM jobs WHERE id = job_id AND rownum = 1;
if v_responsible_job is not null then
if length(v_responsible_job) > 0 then
v_responsible_job := ', '|| v_responsible_job;
end if;
end if;
end if;
if v_count_respon > 0 and v_count_word = 0 then
SELECT full_name, job INTO v_responsible, job_id FROM physical_persons WHERE id IN (SELECT RESPONSIBLE
FROM ADRESSES
WHERE name = p1.name) AND rownum = 1;
SELECT name INTO v_responsible_job FROM jobs WHERE id = job_id AND rownum = 1;
if v_responsible_job is not null then
if length(v_responsible_job) > 0 then
v_responsible_job := ', '|| v_responsible_job;
end if;
end if;
end if;
if v_count_respon = 0 and v_count_word = 0 then
SELECT full_name, job INTO v_responsible, job_id FROM physical_persons WHERE id IN (SELECT physical_person FROM data_separators WHERE id = way.DS_ID) AND rownum = 1;
SELECT name INTO v_responsible_job FROM jobs WHERE id = job_id AND rownum = 1;
if v_responsible_job is not null then
if length(v_responsible_job) > 0 then
v_responsible_job := ', '|| v_responsible_job;
end if;
end if;
end if;
EXCEPTION WHEN OTHERS THEN
v_responsible_job := '-';
END;
Combine the first two queries using EXISTS and then use LISTAGG to aggregate the values:
DECLARE
v_responsible_jobs VARCHAR2(4000);
BEGIN
SELECT LISTAGG( name, ',' ) WITHIN GROUP ( ORDER BY name )
INTO v_responsible_jobs
FROM jobs j
WHERE EXISTS(
SELECT 1
FROM physical_persons pp
WHERE pp.job_id = j.job_id
AND id IN (
SELECT physical_person
FROM data_separators
WHERE id = way.DS_ID
)
);
END;
/
An other way would be to use BULK COLLECT INTO a collection data type and then to iterate over the collection concatenating the string; but that is more complicated than using LISTAGG to do all the work for you.
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