Merge with dual not working in 12c - oracle

Hi I am using Oracle 12c and I have executed the below query which returns 0 rows merged. Please advise if I am doing something wrong.
The table cons_temp contains two columns,
name, value.
merge into cons_temp tb
using (select 'Freez' as NAME, 'NOI' as VALUE from dual where 1=2) v
on (tb.NAME=v.NAME)
when matched then update set tb.VALUE=v.VALUE
when not matched then insert (NAME,VALUE) values(v.NAME, v.VALUE);

Why have you put this line in the USING query?
where 1=2
That will always evaluate to false (because 1 != 2). Consequently your USING query will return zero rows, so there is nothing to match or unmatch.
If you remove that WHERE clause your statement will do something.

Related

Update query is working on all rows even after selecting the particular values in oracle

I have written a query where I want to just update some of the LINK_ID. But it is updating all the rows in that table.
Here is my query
UPDATE APP_FIBERINV.TBL_FIBER_INV_CMPAPPROVED_INFO
SET NE_LENGTH =
(select MAINT_ZONE_NE_SPAN_LENGTH from APP_FIBERINV.TBL_FIBER_INV_JOBS WHERE LINK_ID IN ('MORV_1020','ANND_1017','BBSR_1047','DLHI_5417','MYSR_0104'));
I still doubt that the update statement you have posted updates all rows in the table. It must throw an error
ORA-01427: single-row subquery returns more than one row
instead, because your subquery returns five rows where it must be one, as you must find one value for each row you want to update.
This means your subquery is wrong. It selects five rows, where it must select one. You don't want to find the five values for 'MORV_1020', 'ANND_1017', but the one value for the link ID of the row you are updating.
You also want to update certain rows (those with the five link IDs), so you must add a WHERE clause at the end of your update statement.
UPDATE app_fiberinv.tbl_fiber_inv_cmpapproved_info i
SET ne_length =
(
SELECT j.maint_zone_ne_span_length
FROM app_fiberinv.tbl_fiber_inv_jobs j
WHERE j.link_id = i.span_link_id
)
WHERE span_link_id IN ('MORV_1020', 'ANND_1017', 'BBSR_1047', 'DLHI_5417', 'MYSR_0104');
Assuming both tables share the LINK_ID as primary and foreign key, you could just use a MERGE:
MERGE INTO APP_FIBERINV.TBL_FIBER_INV_CMPAPPROVED_INFO APPR_NFO
USING (
SELECT LINK_ID, MAINT_ZONE_NE_SPAN_LENGTH
FROM APP_FIBERINV.TBL_FIBER_INV_JOBS
WHERE LINK_ID IN ('MORV_1020','ANND_1017','BBSR_1047','DLHI_5417','MYSR_0104')
) INV_JOBS
ON ( APPR_NFO.SPAN_LINK_ID = INV_JOBS.LINK_ID)
WHEN MATCHED THEN UPDATE SET APPR_NFO.NE_LENGTH = INV_JOBS.MAINT_ZONE_NE_SPAN_LENGTH;

How do I get N rows in Oracle SQL SELECT statement given number by variable?

In Oracle SQL, I have a variable that is the number of rows to fetch, I can only use SQL SELECT statement, so no PL/SQL block.
If the variable has a value set I must fetch the number of rows in the variable, if not then fetch as many rows as possible (infinite).
I tried:
select * from system_options
THEN FETCH FIRST
CASE :lim
THEN :lim
ELSE 9999
END
ROWS ONLY
This gives me a ORA-00933: SQL command not properly ended.
Another option would be by not having the variable set then not having the THEN FETCH statement.
You have several syntax errors:
You do not need the THEN before the FETCH FIRST; and
The CASE expression is missing a WHEN clause.
I don't have SQL/Plus to hand to test using a bind variable but something like this:
select * from system_options
FETCH FIRST CASE WHEN :lim IS NOT NULL THEN :lim ELSE 9999 END ROWS ONLY
Or, you can use COALESCE:
select * from system_options
FETCH FIRST COALESCE( :lim, 9999 ) ROWS ONLY
db<>fiddle here

can we have two matched clauses in merge

Write a single query that Delete the records whose grades are more than 150. Update grades and add 25 to each as internals if records exist. Insert the records if record does not exists with grade as 25.
Merge into employeegrades empg
using (select id, name from temp_emp) e
on (empg.id= e.id)
when matched then
update set empg.grades = empg.grades + 25
delete where empg.grades > 150
when not matched then insert (id,grades) values (e.id, 25);
This is working fine but when I modify the statement as below, it is not working:
merge into employeegrades eg
using (select id, name from temp_emp) emp
on (eg.id = emp.id)
when matched and eg.grades > 150 then
delete
when matched
update set eg.grades = eg.grades+25
when not matched then
insert (id,grades)
values (emp.id, 25);
what is wrong with the above query? Can't we specify two matched conditions in merge?
"Can't we specify two matched conditions in merge?"
The syntax rules in the Oracle documentation specify two clauses:
the merge_update_clause which executes updates and/or deletes against existing rows
the merge_insert_clause which executes inserts of new rows
That is, the clauses are defined in terms of the actions they implement, not whether they are MATCHED or NOT MATCHED. Because it wouldn't make sense to insert when MATCHED or update when NOT MATCHED.
So, the WHEN MATCHED THEN keyword phrase can only appear once in a MERGE statement because the syntax allows only one merge_update_clause.

Update using select query with multiple Rows in Oracle

Can any one please help me to solve this issue
Table Name:RW_LN
LN_ID RE_LN_ID RE_PR_ID
LN001 RN001 RN002
LN002 RN002 RN003
LN003 RN003 RN001
LN004 RN001 RN002
MY Update Query is:
update table RW_LN set RE_LN_ID=(
select LN_ID
from RW_LN as n1,RW_LN as n2
where n1.RE_LN_ID = n2.RE_PR_ID)
MY Expected Result is:
LN_ID RE_LN_ID
LN001 LN003
LN002 LN004
LN003 LN002
LN004 LN003
This above query shows error as SUB QUERY RETURNS MULTIPLE ROWS.Can any one provide the solution for this, I am Beginner in Oracle 9i.So Stuck in the logic
you can try to solve this with a distinct
update table RW_LN set RE_LN_ID=(
select distinct LN_ID
from RW_LN as n1,RW_LN as n2
where n1.RE_LN_ID = n2.RE_PR_ID)
if that still returns multiple rows, it means you are missing a join somewhere along the way or potentially have a bad schema that needs to use primary keys.
If you want to take the "biggest" corresponding LN_ID, you could do
update RW_LN r1
set r1.RE_LN_ID = (select MAX(LN_ID)
FROM RW_LN r2
where r1.RE_LN_ID = r2.RE_PR_ID);
see SqlFiddle
But you should explain why you choose (as new RE_LN_ID) LN004 instead of LN001 for LN_ID LN002 (cause you could choose both)
Just guessing, but possibly this is what you want.
update
RW_LN n1
set
RE_LN_ID=(
select n2.LN_ID
from RW_LN n2
where n1.RE_LN_ID = n2.RE_PR_ID)
where exists (
select null
from RW_LN n2
where n1.RE_LN_ID = n2.RE_PR_ID and
n2.ln_id is not null)
At the moment there is no correlation between the rows you are updating and the value being returned in the subquery.
The query reads as follows:
For every row in RW_LN change the value of RE_LN_ID to be:
the value of LN_ID in a row in RW_LN for which:
the RE_PR_ID equals the original tables value of RE_LN_ID
IF there exists at least one row in RW_LN for which:
RE_PR_ID is the same as RE_LN_ID in the original table AND
LN_ID is not null

Update One table Column with Values from Another table Having Similar

Hi Guys I have Two tables (MIGADM.CORPMISCELLANEOUSINFO and CRMUSER.PREFERENCES) and Each Has a field called PREFERENCE_ID and ORGKEY. I want to Update the Preference ID for MIGADM.CORPMISCELLANEOUSINFO with Preference_ID from CRMUSER.PREFERENCES for Each Corresponding ORGKEY. SO I wrote this Query;
update migadm.CORPMISCELLANEOUSINFO s set s.PREFERENCE_ID = (
select e.PREFERENCE_ID from crmuser.preferences e where s.ORGKEY = e.ORGKEY)
But I get:
ORA-01427: single-row subquery returns more than one row
What Should I do?
It means the columns you have selected are not unique enough to identify one row in your source table. Your first step would be to identify those columns.
To see the set of rows that have this problem, run this query.
select e.origkey,
count(*)
from crmuser.preferences e
group by e.origkey
having count(*) > 1
eg : for origkey of 2, let's say there are two rows in the preferences table.
orig_key PREFERENCE_ID
2 202
2 201
Oracle is not sure which of these should be used to update the preference_id column in CORPMISCELLANEOUSINFO
identify the row where the subquery returns more than one row (You could use REJECT ERROR clause to do it for instance) or use the condition 'where rownum = 1'.

Resources