Update from two tables oracle [duplicate] - oracle

This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 4 years ago.
I have two tables.
J_YAD_PICT (table name) , GALLERY_DISP_FLG (column)
J_YAD_CGR_PICT (table name) , Y_CGR_FLG (column)
when Y_CGR_FLG not null in J_YAD_CGR_PICT, then GALLERY_DISP_FLG needed to set as 1.
I have executed this
Update A
SET A.GALLERY_DISP_FLG =
(CASE WHEN b.Y_CGR_FLG IS NOT NULL) THEN '1'
ELSE A.GALLERY_DISP_FLG
END
FROM J_YAD_PICT A inner join J_YAD_CGR_PICT B
on A.YP_ID = B.YP_ID;
But given this error :
SQL Error: ORA-00905: missing keyword
Could anyone help me ?

which table you use alias A?
i suggest try like this
Update J_YAD_PICT A SET
A.GALLERY_DISP_FLG = (CASE WHEN b.Y_CGR_FLG IS NOT NULL) THEN '1'
ELSE A.GALLERY_DISP_FLG
END
FROM J_YAD_PICT A inner join J_YAD_CGR_PICT B on A.YP_ID = B.YP_ID;

Something like this?
update j_yad_pict p set
p.gallery_disp_flg = (select case when c.y_cgr_flg is not null then '1'
else p.gallery_disp_flg
end
from j_yad_cgr_pict c
where c.yp_id = p.yp_id
)
where exists (select null from j_yad_cgr_pict c1
where c1.yp_id = p.yp_id
);
EXISTS part of the query is here so that you wouldn't update rows (to NULL) that don't have a match in J_YAD_CGR_PICT table.

When update table J_YAD_PICT, must be this table connect from table in inner select
UPDATE J_YAD_PICT A
SET A.GALLERY_DISP_FLG = (
SELECT CASE WHEN b.Y_CGR_FLG IS NOT NULL THEN '1' ELSE A.GALLERY_DISP_FLG END
FROM J_YAD_PICT A2
INNER JOIN J_YAD_CGR_PICT B ON A.YP_ID = B.YP_ID
WHERE A.ID = A2.ID);

Related

Update statement in Oracle with joins

I have the below code in SQL Server:
UPDATE RH SET
--SELECT RH.economic_class ,
economic_class = C.economic_class
FROM dbo.tblRecHist RH
JOIN tblComp C ON RH.company_id = C.company_id
WHERE RH.company_id = #CompID
AND RH.rec_date = #RecDate
Which I have converted in Oracle to:
UPDATE tblRecHist
SET economic_class = (Select C.economic_class from tblComp C join tblRecHist
RH on RH.company_id = C.company_id)
WHERE RH.company_id = v_CompID
AND RH.rec_date = v_RecDate;
As this is a part of a big procedure I am unable to execute this independently.
So, my question is are both the logic correct? If not, what could be the possible change?
That logic won't work becuase your subquery will return more than one record. Instead of joining tblRecHist into your subquery, omit it and correlate the subquery with the current row of tblRecHist from the UPDATE.
UPDATE tblRecHist rh
SET economic_class = (Select C.economic_class
from tblComp C
WHERE C.company_id = RH.company_id )
WHERE RH.company_id = v_CompID
AND RH.rec_date = v_RecDate;
This assumes company_id is a unique key in tblComp.

cant update a column from table it returns single-row subquery returns more than one

When i execute the following query, i get the message like
Ora-01427 single-row subquery returns more than one row
I am trying to update "City" column in Table A From another table.
How would I do this?
table A: Name, PER_code(it also has duplicated value or null), city, PD_code
table B: Name, PER_code(No duplicated value,Maybe null), city, Postal_code
The update statement:
UPDATE A
SET (A.city) =
(SELECT B.city
FROM B
INNER JOIN A
ON A.per_code=B.per_code
WHERE A.per_code is not null)
Since there are duplicate values but you select only one field, you modify your query from
UPDATE A SET (A.city) = (SELECT B.city FROM B INNER JOIN A ON
A.per_code=B.per_code WHERE A.per_code is not null)
to
UPDATE A SET (A.city) = (SELECT DISTINCT B.city FROM B INNER JOIN A ON
A.per_code=B.per_code WHERE A.per_code is not null)
The distinct operator will allow you to keep a single value if it's duplicated in the table B. If there are multiple distinct values, you will have to look at your data and make a decision about which value should be used in the other table.
You can also try MERGE INTO selecting DISTINCT records.
MERGE INTO A d
USING
( SELECT DISTINCT per_code, city FROM B ) s
ON ( s.per_code = d.per_code )
WHEN MATCHED THEN UPDATE SET d.City = s.City
WHERE d.per_code IS NOT NULL;
I think you intend a correlated subquery:
UPDATE A
SET city = (SELECT B.city
FROM B
WHERE A.per_code = B.per_code
)
WHERE A.per_code is not null;
EDIT:
The above should work given the constraints in the original question. If it does not, it is easily adaptable:
UPDATE A
SET city = (SELECT B.city
FROM B
WHERE A.per_code = B.per_code AND rownum = 1
)
WHERE A.per_code is not null;

Insert Records in a Table without duplicating the records

I have this MySQL Statement. I want to execute this SQL repeatedly to insert the result into a Third table withoud duplicating the Records in Third Table.
SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID
and Client_Check_Out = CURDATE() )
AND NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID
and Client_Check_In = CURDATE())
I am attempting following Statement but it is throwing error
INSERT IGNORE INTO cio_alert (`Client_ID`, `Client_RFID_Number`, `Client_Check_Out`, `Client_Check_In`)
SELECT `ciom_master`.`Client_ID`, `ciom_master`.`Client_RFID_Number`,`cio_master`.`Client_Check_Out`, `cio_master`.`Client_Check_In` FROM ciom_master
INNER JOIN `cio_master` ON `ciom_master`.`Client_ID` = `ciom_master`.`Client_ID`
WHERE EXISTS
(SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID
and Client_Check_Out = CURDATE() )
AND NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID
and Client_Check_In = CURDATE())
AND (
SELECT `Client_Check_Out` FROM cio_master
WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL)
OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))
AND (
SELECT `Client_Check_In` FROM cio_master
WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL)
OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))
)
You didnt mention exist or not exist condition at the last select statement... u simply put the and condition without any exist or not exist or in or not in. Try to put any of the valid condition over there. And also in the first select statement y r u using same table twice and joining without any alias try to check that one also once.

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)

Need a fix for merge update query (Simple but still confusing)

MERGE INTO table_1 a
USING
(SELECT * from table_2) b ON ( a.row_id = b.row_id and a.in_correct IS NULL)
WHEN MATCHED THEN UPDATE SET a.in_correct = 'Y';
In the above query ORA-38104:Column referenced in ON clause cannot be updated.
I have been sitting for hours to resolve this.
I have identified that the problem is the field in_correct.
This field "in_correct" cannot be put in both ON clause and also after SET. But in order satisfy my criteria,I have no option.
Please help me out
MERGE INTO table_1 a USING
(SELECT * from table_2) b
ON ( a.row_id = b.row_id)
WHEN MATCHED THEN UPDATE
SET a.in_correct = NVL(in_correct, 'Y');
UPDATE:
A more "general" command (for non null values):
MERGE INTO table_1 a USING
(SELECT * from table_2) b
ON ( a.row_id = b.row_id)
WHEN MATCHED THEN UPDATE
SET a.in_correct = case
when in_correct = 'valuetobereplaced' then 'Y';
else in_correct;
end;

Resources