suppress oracle data not found exception - oracle

I have a unique scenario where-in i need multiple column values to be put into multiple variables. The problem i am facing is that while one column value is present the others needn't be present, hence i end up with DATA NOT FOUND exception, while i want to suppress it and put empty values into the remaining variable.
select nvl(A,''), nvl(B,''), nvl(C,'')
into A1, B1, C1
from SAMPLE_TABLE
where name ='W6';
Value of A in the table can be 'hello', Value of B is null and Value of C is null in the table.
When the statement is executed inside the body of a stored proc i do not want the DATA NOT FOUND Exception, instead i want A1 to have the value 'hello', B1 as '' and C1 as ''. I will be running this dynamically and the where condition keeps changing, hence i do not want to open those many cursors either. Can anyone please let me know how i can accomplish the same?

Your analysis isn't quite correct. You only receive the DATA NOT FOUND error if the whole row is missing, i.e. the WHERE condition name ='W6' doesn't select any rows.
To avoid the error, you can use exception handling:
BEGIN
select A, B, C
into A1, B1, C1
from SAMPLE_TABLE where name ='W6';
EXCEPTION
WHEN NO_DATA_FOUND THEN
A1 := 'hello';
B1 := NULL;
C1 := NULL;
END;
Update:
If you want to select a NULL values even if the WHERE condition matches no row, then you can try the following query:
SELECT t.A, t.B, t.C
FROM DUAL
LEFT JOIN SAMPLE_TABLE t ON t.name = 'W6';
Update 2: Query with exactly one row:
This query should always return a single row:
SELECT A, B, C
INTO A1, B1, C1
FROM (
SELECT t.A, t.B, t.C
FROM DUAL
LEFT JOIN SAMPLE_TABLE t ON t.name = 'W6'
) x
WHERE ROWNUM <= 1;

Related

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

ORA-00918: column ambiguously defined..How do i resolve this error?

Here is the query:
select
t1_c1,
case
when max(t1_c2) = 'X'
then max(t2_c3)
else 'X ' || max(t2_c3)
end as t2_c3
from
t1 a1,
t2
where t1_c4 =
(
select max(a2.t1_c4)
from t1 a2
where a1.t1_c1 = a2.t1_c1
and a2.t1_c4 <= '31-AUG-'||&ws_acad_yr
)
and t1_c2 = t2_c8(+)
group by t1_c1
)
where c1 = t3_c1
and t3_c5 is null
and c1 = t4_c1
and t4_c6 = t5_c8(+)
and t4_c5 = t6_c8(+)
and c1 = t7_c1(+)
and c1 = t8_c1(+)
and c1 = t1_c1(+)
I am using pidm column from two or more table..but i am not sure how to resolve this? I am not sure how and where to prefix the column or not. I am a new user to this.
When you need to distinguish between two columns that have the same name you can provide a prefix with the table it belongs to:
SELECT line.total, invoice.total
FROM invoice
INNER JOIN line ON invoice.invoice_id=line.invoice_id
But that's not always possible/desirable:
It isn't possible it the column comes from a subquery
It isn't useful if you read the same table twice
It isn't practical if table name is too long
To solve this, SQL allows to define aliases. You are already using them!
from t1 a1
^ ^
| \ Alias
\ Table
ORA-00918: column ambiguously defined means that you have a column name that belongs to more than one column and Oracle doesn't know which one you mean. The complete error message possibly tells you the column name and the exact position where you are using it. Add the appropriate table/alias prefix and you'll be done.

Hive command to execute NOT IN clause

I have two tables,tab1 & tab2.
tab1(T1) tab2(T2)
a1 b1
b1 c1
c1 f1
d1 g1
I am looking for the values from table T1 that are not present in T2.
In this case, the output should be a1 d1
I have tried with the following query but couldn't get the right solution.
select distinct tab1.T1 from tab1 left semi join tab2 on (tab1.T1!=tab2.T2);
SELECT t1.str
FROM tab1 t1
LEFT OUTER JOIN tab2 t2 ON t1.str = t2.str
WHERE t2.str IS NULL;
Result:
OK
a1
d1
"Why is the t2.str is null condition there": Left outer joins ensure that all values from the first table are included in the results. So then what happens when there are no values in the second table: in that case all of the columns from the second table are reported as null.
So in the case above we are searching precisely for the condition that the second table entries are missing - and thus we:
Choose one of the never-empty (aka not null) columns from table two.
So: is number an always-present column? If not then please choose another one
Specify the condition "table1-alias"."table1-never-null-column" = null. That means that the record is actually not present in the join condition - and thus we found the records existing only in table 1.

ORA-01791 Pl-Sql error

hi guy i have a query that give me the followin error:
ORA-01791: not a SELECTed expression
this is the select expresison , please can you tell me why ?
declare
freqLettura varchar2(64);
billingcy varchar2(64);
begin
freqLettura := null;
billingcy := null;
for rec in ( select distinct(fn_get_facilityid(z.uidfacility) ) as a, 1 as b
from facilityhistory z,
locality l ,
plant p ,
ztmp_sam_tb_sdv zsdv ,
ztmp_sam_tb_plantcode zplant ,
sam_tb_ca_pdr sam,
meterhistory mh,
meter m ,
meterclass mc
where
Z.UIDLOCALITY = L.UIDLOCALITY and
p.UIDPLANT = L.UIDPLANT and
z.uidaccount = zsdv.uidaccount and
p.plantcode = zplant.plantcode and
sam.uidfacility = z.uidfacility and
z.stoptime is null and
sam.status = 'U' and
mh.uidfacility = z.uidfacility and
mh.uidmeter = m.uidmeter and
m.uidmeterclass = mc.uidmeterclass and
(billingcy is null or p.UIDBILLINGCYCLE = billingcy )
AND
(
(
(freqLettura = 'G') AND ( mh.corrmeterid is not null and mh.stoptime is null and mc.maxflowmeter >= SAM_FN_GET_PARAMETER_FLOAT('MAXFLOWMET_DETT_GIORN'))
)
OR
(
nvl(freqLettura,'nullo') <> 'G' AND (freqLettura is null or sam.readfrequency = freqLettura)
)
) and ROWNUM = 1 order by sam.stoptime, sam.uidsamtbpdr desc ) loop
begin
insert into ztmp_sam_tb_elab_pdr (facilityid, uidbatchrequest) VALUES (rec.a, rec.b);
exception
when dup_val_on_index then
null;
end;
end loop;
end;
Whenever you get an Oracle error message you don't understand, the first thing to do is look up the meaning. One way is simply to Google it. In this case the full description found in
Oracle9i Database Error Messages is:
ORA-01791 not a SELECTed expression
Cause: There is an incorrect ORDER
BY item. The query is a SELECT DISTINCT query with an ORDER BY clause.
In this context, all ORDER BY items must be constants, SELECT list
expressions, or expressions whose operands are constants or SELECT
list expressions.
Action: Remove the inappropriate ORDER BY item from the SELECT list
and retry the statement.
(Oddly this error message isn't documented in the 10G or 11G manuals, despite still being raised!)
This matches the statement you have written, which is a SELECT DISTINCT query where you are trying to order the results by a column that you did not select.
If you think about it, what you are asking for doesn't make sense: by selecting DISTINCT values that do not include sam.stoptime (for example) you may be consolidating many rows with different values for sam.stoptime, so which one would govern the ordering?
Also, as Noel's answer points out, there is no reason to have an ORDER BY clause in this code anyway, so the solution is simply to remove it.
If you are using DISTINCT in your SELECT query, then your ORDER BY clause should contain only those columns that your selecting. In this case sam.stoptime, sam.uidsamtbpdr are not there in SELECT statement. You can remove the ORDER BY clause, as it is not doing anything useful in your example.

PL/SQL: Loop through and alter REFCURSOR rows before returning to calling procedure

I have a refcursor in a function, declared like this:
my_cursor type_refcur_my
And populated as such:
OPEN my_cursor FOR
SELECT DISTINCT A.vegetable, A.animal, A.mineral, A.ID,
(SELECT DISTINCT SUBSTR(bcptr.bcptr_desc_l1,INSTR(bcptr.bcptr_desc_l1,')',-1)-3,3)
FROM doe D, ray R, me M
WHERE ...) ID
FROM artifacts A
ORDER BY vegetable, mineral;
RETURN my_cursor;
I need to perform an operation that involves another SELECT on all the rows in the recursor and use some logic to alter 2 of the column values before returning it. Something kind of like:
IF my_cursor.vegetable = (SELECT B.ID from vegetables B
WHERE my_cursor.vegetable = B.vegetable_description)
THEN
my_cursor.A.ID := B.ID
END IF;
My thought was to put this code after opening the cursor and before returning it. But this produces compile errors, and I cannot find an appropriate example online.I appreciate your help.

Resources