Oracle SQL Compiles, but seems to break out of loop when select loop returns nothing/empty - oracle

I am still quite new to pl/sql... but here I have a script that works well when data exists...But I also need it to work when the select statement does NOT have any value to return (is empty). When the select statement is empty, dbms_output.put_line does not show anything, even when I write a complete string there, so I think the loop is exiting when nothing is found. Online, I stumbled upon Union and Exception...but nothing that seems to work accurately in a loop.
WHAT I AM ASKING IS: How to run this script even when data DOES NOT EXIST??? AND How do I tell which value is empty? Is it the postal_code or the fipcode, or BOTH?
DECLARE
F33_35 VARCHAR2(3) := rpad('0',3,'0');
BEGIN
FOR tabVals IN (SELECT DISTINCT pa.postal_code, fip.fipcode
FROM voucher v
INNER JOIN cc_authorization cca
ON cca.cc_authorization_id = v.cc_authorization_id
INNER JOIN episode e
ON cca.subsidy_id = e.episode_id
INNER JOIN case c
ON c.case_id = e.case_id
INNER JOIN invoice_item ii
ON ii.voucher_id = v.voucher_id
INNER JOIN invoice i
ON i.invoice_id = ii.invoice_id
INNER JOIN svcperiod_info s
ON s.svcperiod_id = i.svcperiod_id
INNER JOIN party_contact_mech pcm
ON pcm.party_id = c.identifying_party
INNER JOIN postal_address pa
ON pcm.contact_mech_id = pa.contact_mech_id
INNER JOIN VT_FIPS_MAP fip
ON substr(pa.POSTAL_CODE,1,5) = substr(fip.ZIPCODE,1,5)
WHERE ii.status = 8 AND
ii.net_amount > 0 AND
s.end_dt >= DATE'2017-04-01' AND
s.end_dt < DATE'2017-04-30' AND
c.case_id = 104570 AND
pcm.contact_mech_purpose_cd = 4 AND
pcm.current_ind = 1
ORDER BY POSTAL_CODE, FIPCODE)
LOOP
F33_35 := substr(F33_35 || tabVals.fipcode,-3);
DBMS_OUTPUT.PUT_LINE('F String: 50' || F33_35);
--- RESET certain variables for loop
F33_35 := rpad('0',3,'0');
END LOOP;
END;

you are correct when you say
...I think the loop is exiting when nothing is found...
the cursor for loop can be considered a special case of regular loop.
eg.
for a in (<select statement>) LOOP
--do stuff here
END LOOP;
is effectively just shorthand for
declare
cursor c is
<select statement>;
a c%ROWTYPE;
begin
open c;
LOOP
FETCH c INTO a;
EXIT WHEN c%NOTFOUND;
--do stuff here
END LOOP;
so you can no see that no code would be executed if the query returns 0 rows.
possible the best solution for running code if your statement returns 0 rows is to set a flag inside the loop and check it afterwards. (as suggested by #tony-Andrews )
a second option is to rewrite as the long version of the loop above and move the EXIT WHEN statement to after the "do stuff"

Related

Update using loop in Oracle

BEGIN
FOR J IN (select DISTINCT S_NUMBER from WMWHSE3.NUMBER_SHIPTO)
LOOP
UPDATE wmwhse3.orders
SET
orders.susr4 = (select S_NUMBER from WMWHSE3.NUMBER_SHIPTO where S_NUMBER = J)
where orders.c_company = (select SHIPTO from WMWHSE3.NUMBER_SHIPTO where S_NUMBER = J)
and orders.orderkey in (SELECT a.orderkey FROM wmwhse3.orders a INNER JOIN wmwhse3.wavedetail b ON a.orderkey = b.orderkey where b.wavekey = '0000000086');
END LOOP;
END;
Looks like you're overcomplicating things (and making everything slower than it should be, as row-by-row processing (in a loop) is usually much slower than set processing).
Therefore, how about a single update, without PL/SQL? Something like this:
update orders o set
o.susr4 = (select n.s_number
from number_shipto n
where n.shipto = o.c_company
)
where exists (select null from wavedetail b
where b.orderkey = o.orderkey
and b.wavekey = '0000000086'
);

Iterate through cursor and storing the output of the cursor in another table

I am trying to iterate through a cursor which stores the value of the table. I use a FOR Loop to iterate and IF one of the conditions is met, I store the output in another table. I am not sure of the approach I am following and also getting error(ORA-00933: SQL command not ended properly). Stats_Queries is my reference table where I iterate my cursor through. STATS_RESULT_CARD is my output table where I have to store the results. Please help.
DECLARE
CURSOR c1 IS
select Stats_Queries.OBJECTTYPE, Stats_Queries.CATEGORY, Stats_Queries.QUERY
from Stats_Queries;
r1 c1%ROWTYPE;
BEGIN
FOR r1 IN c1 LOOP
If (r1.OBJECTTYPE = 'CARD') THEN
INSERT INTO STATS_RESULTS_CARD (NODETYPENAME, NODEDEFNAME , CARDTYPENAME, PROVISIONSTATUSNAME, STATDATE, CARDCOUNT)
select nt.name, nd.name, ct.name, ps.name, sysdate, count(c.cardid)
from cardtype ct, card c, node n, nodetype nt, nodedef nd, provisionstatus ps
where ct.name in ('SRA AMP', 'XLA AMP', 'SAM', 'ESAM')
and ct.cardtypeid = c.card2cardtype
and c.card2node = n.nodeid
and n.node2nodetype = nt.nodetypeid
and n.node2nodedef = nd.nodedefid
and c.card2provisionstatus = ps.provisionstatusid
group by nt.name, nd.name, ct.name, ps.name
END If;
END LOOP;
END;
As an aside from the answer that Finbarr has provided (which is perfectly correct; add in the missing semi-colon and your procedure should work), why do you need to loop through the cursor at all? That's the slow way of doing it.
You could just do a single insert statement instead, such as:
insert into stats_results_card (nodetypename,
nodedefname,
cardtypename,
provisionstatusname,
statdate,
cardcount)
select x.nt_name,
x.nd_name,
x.ct_name,
x.ps_name,
x.statdate,
x.cnt_cardid
from (select nt.name nt_name,
nd.name nd_name,
ct.name ct_name,
ps.name ps_name,
sysdate statdate,
count (c.cardid) cnt_cardid
from cardtype ct,
card c,
node n,
nodetype nt,
nodedef nd,
provisionstatus ps
where ct.name in ('SRA AMP',
'XLA AMP',
'SAM',
'ESAM')
and ct.cardtypeid = c.card2cardtype
and c.card2node = n.nodeid
and n.node2nodetype = nt.nodetypeid
and n.node2nodedef = nd.nodedefid
and c.card2provisionstatus = ps.provisionstatusid
group by nt.name,
nd.name,
ct.name,
ps.name) x
cross join (select stats_queries.objecttype,
stats_queries.category,
stats_queries.query
from stats_queries
where objecttype = 'CARD');
N.B. This assumes that there really isn't any link between the original cursor and the select statement that was inside the loop; we do a cross join to replicate the rows the required number of times.
If there was an actual join between the two queries, you would put that in place of the cross join.
ORA-00933: SQL command not ended properly
Probably occurring because you missed a semicolon after
group by nt.name, nd.name, ct.name, ps.name

Oracle Merge statement error in procedure package body

I'm struggling trying to make this procedure to work, I have the following code inside my package body:
PACKAGE BODY PKG_DM_TRANS_DIMENSIONES AS
PROCEDURE SP_DM_TRANS_DIM_CUENTA AS
vNumRegistrosDimCuentas NUMBER;
BEGIN
SELECT COUNT(*) INTO vNumRegistrosDimCuentas
FROM DIM_CUENTAS;
IF (vNumRegistrosDimCuentas <> 0) THEN
MERGE INTO DIM_CUENTAS DIMC
USING (
SELECT * FROM (
SELECT
DIM.FNT_CUENTA_ID AS DIM_CUENTA_ID,
C.CUE_ID AS FNT_CUENTA_ID,
R.REG_REGION AS REGION,
P.PAI_PAIS AS PAIS,
E.EDI_NOMBRE_EDIFICIO AS EDIFICIO,
C.CUE_CUENTA,
TIC.TIC_TIPO_CONTACTO,
C.CUE_STATUS,
CASE
WHEN DIM.FNT_CUENTA_ID IS NULL THEN 1
WHEN
R.REG_REGION <> DIM.REGION OR
P.PAI_PAIS <> DIM.PAIS OR
E.EDI_NOMBRE_EDIFICIO <> DIM.EDIFICIO OR
C.CUE_CUENTA <> DIM.CUENTA OR
TIC.TIC_TIPO_CONTACTO <> DIM.TIPO_CONTACTO
THEN 2
ELSE 0
END AS TIPO_FILA
FROM STA_EDIFICIOS_EXTRACCION E
LEFT JOIN
STA_PAISES_EXTRACCION P ON E.EDI_PAI_ID = P.PAI_ID
LEFT JOIN
STA_REGIONES_EXTRACCION R ON P.PAI_REG_ID = R.REG_ID
LEFT JOIN
EUB_EDIFICIO_UBICACION EUB ON EUB.EUB_EDI_ID = E.EDI_ID
LEFT JOIN
STA_CUENTAS_EXTRACCION C ON C.CUE_EUB_ID = EUB.EUB_ID
LEFT JOIN
STA_TIPOS_CONTACTO_EXTRACCION TIC ON TIC.TIC_ID = C.CUE_TIC_ID
LEFT JOIN
DIM_CUENTAS DIM ON
(C.CUE_ID = DIM.FNT_CUENTA_ID AND DIM.CUENTA_STATUS = 1)
)
) Q
ON (DIMC.FNT_CUENTA_ID = Q.TIPO_FILA)
WHEN MATCHED THEN
INSERT (DIMC.REGION, DIMC.PAIS, DIMC.EDIFICIO, DIMC.CUENTA, DIMC.TIPO_CONTACTO, DIMC.CUENTA_FECHA_CREACION, DIMC.FNT_CUENTA_ID)
VALUES (Q.REGION, Q.PAIS, Q.EDIFICIO, Q.CUE_CUENTA, Q.TIC_TIPO_CONTACTO, TO_TIMESTAMP(sysdate, 'MM/DD/YYYY HH24:MI:SS'), Q.FNT_CUENTA_ID)
WHEN NOT MATCHED THEN
UPDATE SET DIMC.CUENTA_STATUS = 0 WHERE DIMC.CUENTA_STATUS = 1 -- <- dummy update stmt
ELSE ..... -- else statement code working fine...
END IF;
END SP_DM_TRANS_DIM_CUENTA;
END PKG_DM_TRANS_DIMENSIONES;
I'm getting erros at the line
MERGE INTO DIM_CUENTAS DIMC
Saying "Statement ignored"
and then, another error at:
INSERT (DIMC.REGION, DIMC.PAIS, DIMC.EDIFICIO, DIMC.CUENTA, DIMC.TIPO_CONTACTO, DIMC.CUENTA_FECHA_CREACION, DIMC.FNT_CUENTA_ID)
VALUES (Q.REGION, Q.PAIS, Q.EDIFICIO, Q.CUE_CUENTA, Q.TIC_TIPO_CONTACTO, TO_TIMESTAMP(sysdate, 'MM/DD/YYYY HH24:MI:SS'), Q.FNT_CUENTA_ID)
saying "missing keyword". Is it possible to use the merge statement in a SP? I'm new to Oracle so I really don't know if what I'm trying to do is possible or if there's something wrong with my code.
Thanks for any help, I would really appreaciate it.
I think that you swapped commands - after when matched you should put update statement and after not matched - insert.
Similar example worked for me, but after swapping statements I got ORA-00905 missing keyword. So correct version is:
merge into t1
using (select * from t2) t2 on (t1.id = t2.id)
when matched then update set t1.name = t2.name
when not matched then insert (id, name) values (t2.id, t2.name)

How to loop in sql?

I dont want to use the "loop" related keyword, how can I implement loop with basic sql command in oracle ?
I have two table :
A:
ID, Color
B,
ID, AID, Type
I want to loop all records in B, and if ID = AID, then set the A.Color = B.Type
Thanks in advance !
Looping is, by definition, a procedural construct.
SQL is declarative: tell the database what you want done, not how to do it.
If you're absolutely convinced that you need to program such a thing, then write it in PL/SQL, Oracle's procedural language.
Bu I'm sure that it's possible to do what you want in SQL using an UPDATE with a WHERE clause.
Something like this (corrected per NullUserException):
UPDATE A SET A.Color = (SELECT B.Type FROM B WHERE A.ID = B.AID)
An alternate method:
MERGE INTO a
USING b
ON (b.aid = a.id)
WHEN MATCHED THEN UPDATE SET a.color = b.type;
You could just do:
UPDATE tablea a
SET a.color = (SELECT b.type
FROM tableb b
WHERE b.aid = a.id)
See this SQL script.
To do that you will have to write a stored procedure using PL/SQL. Here is the oracle page with some info and papers on the topic.
As others pointed out, you can probably solve your problem with a normal DML statement, without any looping involved. But to give you some basics on how to accomplish what you asked for in PL/SQL, here's an example...
DECLARE
CURSOR c IS
SELECT id, aid, type FROM b;
statement VARCHAR2(200);
BEGIN
FOR iterator IN c LOOP
IF iterator.id = iterator.aid THEN
statement := 'UPDATE a SET color = ' || iterator.type || 'WHERE id = ' || iterator.id;
EXECUTE IMMEDIATE statement;
END IF;
END LOOP;
END;
This anonymous PL/SQL block will iterate through each record in table b, and if b.id = b.aid, it will update table a and set a.color = b.type where a.id = b.id.
This seems to be what you were asking for. It's not exactly an efficient way to go about doing things, since you're firing off one DML statement per row in table b that has b.id=b.aid. But I wanted more to give this as a syntax example. This is just one way to iterate through a cursor by the way; you can also explicitly open cursors and fetch records, but it's easier this way if you don't need to do anything but iterate over the entire result set.

Run sql code with variables in Oracle SQL Developer code window

I'm writing code using Oracle SQL Developer. I have a simple select statement that works:
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when 1 = 1 then
sl.usbank_to_edit
else
case
when 'ENT\CB174' = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
However I want to add some variables to it and reference the variables in the statement, similar to below, and also run it in the code window. How do I do this correctly?
DECLARE
p_USBank_n NUMBER;
p_user_id_c VARCHAR2(20);
BEGIN
p_USBank_n := 1;
p_user_id_c := 'ENT\CB174';
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when p_USBank_n = 1 then
sl.usbank_to_edit
else
case
when p_user_id_c = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
END;
When I run this in a sql window I get the message below:
Error report:
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
if I am reading this correctly, I think you are looking for Oracle Substitution variables.
this will prompt you each time to input the values, by using &val it will prompt you # runtime
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
CASE
when &p_USBank_n = 1 then
sl.usbank_to_edit
else
CASE
when '&p_user_id_c' = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
CFS.CAE_SEC_ID IN (3741, 3744, 3748, 3752);
change it to &&var to have it retain the value, then use
UNDEFINE var
to clear it
Now you can set these at the top of the page (thus avoiding the prompt) by utilizing DEFINE as such
DEFINE XYZ = 5
DEFINE AAA = to_date('10/10/2010','mm/dd/rrrr')
DEFINE textString = AaBbCc
SELECT &&XYZ b, &&AAA a, '&&textString' textString
from dual ;
B A TEXTSTRING
---------------------- ------------------------- ----------
5 10.OCT.2010 00:00 AaBbCc
--typing define will show you all the "defined" values
define
DEFINE XYZ = "5"
DEFINE TEXTSTRING = "AaBbCc"
DEFINE AAA = "to_date('10/10/2010','mm/dd/rrrr')"
the double ampersand will 'retain' the value until you UNDEFINE it (see above) or redefine it.
The error message explains the problem; inside a PL/SQL block you have to select INTO something, and you can't just dump the results of a query to screen as you can with plain SQL. (There are ways to do it but probably overly complicated for what it looks like you're trying to achieve here).
If you don't want to use substitution variables as #Harrison suggested, you can use bind variables which you define at the start in a separate anonymous block. You can then refer to the bind variable in the plain SQL:
var p_usbank_n number;
var p_user_id_c varchar2(20);
exec :p_usbank_n := 1;
exec :p_user_id_c := 'ENT\CB174';
select
cfs.cae_sec_id,
cfs.fm_sec_code,
cfs.last_user_id,
case
when 1 = :p_usbank_n then
sl.usbank_to_edit
when cfs.last_user_id = :p_user_id_c then
sl.owner_to_edit
else
sl.to_edit
end as canEdit
from
caedbo.cae_fof_security cfs
inner join caedbo.cae_data_set_element cdse_stat
on (cdse_stat.data_set_element_id = cfs.appr_status)
inner join caedbo.cae_state_lookup sl
on (sl.object_state = cdse_stat.data_set_element_id)
where
cfs.cae_sec_id in (3741, 3744, 3748, 3752);

Resources