Update One table Column with Values from Another table Having Similar - oracle

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'.

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;

update table from chilled table with group by in oracle

i have two tables master and other is child. suddenly i have lost voucher_date in master table. now how can i update it from child table where many record inserted against one voucher_number.
i have tried the query
update salem set (vch_date,vch_temp)=(
SELECT
vch_date,
vch_no
FROM sale where salem.vch_no=sale.vch_no GROUP BY vch_no,vch_date);
but i got message
SQL Error: ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
Regard.
GROUP BY you used is, actually, DISTINCT applied to selected column list. It appears that - for a certain vch_no which establishes relation between those two tables - you don't have a distinct vch_date + vch_no combination, which also means that there are several vch_date values for each vch_no. What to do? Pick one, for example maximum.
Also, you're setting salem.vch_temp to sale.vch_no which is pointless as vch_no from sale is equal to vch_no from salem so you can set salem.vch_temp to salem.vch_no.
UPDATE salem m SET
m.vch_date = (SELECT MAX(s.vch_date)
FROM sale s
WHERE m.vch_no = s.vch_no
),
m.vch_temp = m.vch_no;

Strange Behavior of oracle query

I am not an oracle expert. I faced a very strange problem but do not know why this occur.
My query is
SELECT hc.id, hc.owner_name, hc.national_id, hc.phone_no, hc.location, hc.status, hc.expiry_status, od.office_title AS issuer, hc.create_date, hc.email, hc.LATTITUDE, hc.LONGITUDE, hc.HASAD_NO, hc.NUMBERATION, hc.BREEDING_TYPE, hc.PROGENY, hc.office_id, hc.issuer_id, hc.expiry_status, hc.status FROM health_cards hc
LEFT JOIN office_details od ON od.office_id = hc.issuer_id AND od.lang = :lang
WHERE hc.id = :search_data_num OR hc.national_id = :search_data_num or hc.phone_no = :search_data_num OR hc.owner_name LIKE :search_data ORDER BY hc.create_date DESC, hc.id desc OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY
When i m running this query i m getting following error
ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
But if i remove OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY from my query it work perfectly.
I want to know the reason why this query not working with offset statement.
You have repeated column hc.status.
`select 1 as "A" as "A" from dual` - execute OK;
`select * from (select 1 as "A"
, 2 as "A"
from dual);` - ORA-00918: column ambiguously defined
If you and offset, oracle probably does something similar.
You have the column hc.expiry_status twice in your select list.
The problem is that we allow this in a select list, but not within an inline view. When you add the row limiting clause, Oracle transforms the query and the transformation uses an inline view. There is a bug, 13687511 which is marked as fixed.
Meanwhile, the workaround is to either not select it twice, or alias the column(s).

How to add running ID in a single UPDATE statement (Oracle)

let's assume I have a table tab1 in my Oracle DB 12.1, which has a column record_id (type NUMBER) and many other columns, among them a column named exchg_id.
This record_id is always empty when a batch of new rows gets inserted into the table. What I need to do is to populate the record_id with values 1..N for all rows that satisfy a condition ...WHERE EXCHG_ID = 'something' and number of such rows is N. Of course I know how to do this procedurally (in a for-loop), but I'd like to know if there's an faster way using a single UPDATE statement. I imagine something like this:
UPDATE tab1 SET record_id = {1..N} WHERE exchg_id = 'something';
Many thanks for your help!
UPDATE: the order of the rows is not important, I need no specific ordering. I just need unique record_id's 1..N for any given exchg_id.
You could use rownum to set record_id to 1 to N :
UPDATE tab1 SET record_id = rownum WHERE exchg_id = 'something';
If you have some offset, say 10, then use rownum + 10

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

Resources