trying to update a row from one table to another - sql

UPDATE customer_service.batch_status.cust_nm
SET batch_status.cust_nm
SELECT customer_service.batch_detail.cust_nm
FROM customer_service.batch_detail
LEFT JOIN customer_service.batch_status
ON customer_service.batch_detail.cust_nm = customer_service.batch_status.cust_nm;

Your syntax is off.
Instead:
UPDATE customer_service.batch_status.cust_nm
FROM customer_service.batch_detail csbd
SET cust_nm = csbd.cust_nm
WHERE csbd.cust_nm = customer_service.batch_status.cust_nm;

Related

ORA-38104 when trying to update my table using merge

I have a stored procedure in which I want to update some columns, so I wrote below code:
PROCEDURE UPDATE_MST_INFO_BKC (
P_SAPID IN NVARCHAR2
) AS
BEGIN
MERGE INTO tbl_ipcolo_billing_mst I
USING (
SELECT
R4G_STATE, -- poilitical state name
R4G_STATECODE, -- poilitical state code
CIRCLE, -- city name
NE_ID,
LATITUDE,
LONGITUDE,
SAP_ID
FROM
R4G_OSP.ENODEB
WHERE
SAP_ID = P_SAPID
AND ROWNUM = 1
)
O ON ( I.SAP_ID = O.SAP_ID )
WHEN MATCHED THEN
UPDATE SET I.POLITICAL_STATE_NAME = O.R4G_STATE,
I.POLITICAL_STATE_CODE = O.R4G_STATECODE,
I.CITY_NAME = O.CIRCLE,
I.NEID = O.NE_ID,
I.FACILITY_LATITUDE = O.LATITUDE,
I.FACILITY_LONGITUDE = O.LONGITUDE,
I.SAP_ID = O.SAP_ID;
END UPDATE_MST_INFO_BKC;
But it is giving me error as
ORA-38104: Columns referenced in the ON Clause cannot be updated: "I"."SAP_ID"
What am I doing wrong?
You are joining the source to destination tables on I.SAP_ID = O.SAP_ID and then, when matched, are trying to update them and set I.SAP_ID = O.SAP_ID. You cannot update the columns used in the join ... and why would you want to as you have already determined that the values are equal.
Just remove the last line of the UPDATE:
...
O ON ( I.SAP_ID = O.SAP_ID )
WHEN MATCHED THEN
UPDATE SET I.POLITICAL_STATE_NAME = O.R4G_STATE,
I.POLITICAL_STATE_CODE = O.R4G_STATECODE,
I.CITY_NAME = O.CIRCLE,
I.NEID = O.NE_ID,
I.FACILITY_LATITUDE = O.LATITUDE,
I.FACILITY_LONGITUDE = O.LONGITUDE;
The error message tells you what the problem is - a MERGE statement cannot update the columns used in the ON clause - and even tells you what column is the problem: "I"."SAP_ID".
So Oracle hurls ORA-38104 because of this line in your WHEN MATCHED branch
I.SAP_ID = O.SAP_ID;
Remove it and your problem disappears. Fortunately the line is unnecessary: I.SAP_ID already equals O.SAP_ID, otherwise the record wouldn't go down the MATCHED branch.
The reason why is quite straightforward: transactional consistency. The MERGE statement operates over a set of records defined by the USING clause and the ON clause. Updating the columns used in the ON clause threatens the integrity of that set, and so Oracle forbids it.

Access query: Update with a DSum in SET

I get the error "Operation must use an updateable query" when I try to run this SQL code:
UPDATE Progetti
SET Progetti.Eroso = (SELECT sum(Fatture.Fattura) FROM Fatture WHERE Fatture.[Codice Progetto] = Progetti.[Codice Progetto]);
Consider that all the tables, fields and relationships involved exist and are correctly set. The issue (according to me) is that SELECT cannot be inside SET. Is it correct?
So what can be a right solution?
The result must be: for each Progetti.[Codice Progetto] put in the field Progetti.Eroso the sum only of Fatture.Fattura related to Progetti.[Codice Progetto].
An alternative can be:
UPDATE Progetti AS a
SET a.Eroso = DSum("Fattura", "Fatture", "[Codice Progetto]=" & a.[Codice Progetto]);
But I get this warning:
EDIT
I have tried the solution of #user4321:
UPDATE Progetti
SET Progetti.Eroso = Fatture2.FatturaSum
FROM Progetti
Inner Join (SELECT sum(Fatture.Fattura) as FatturaSum
FROM Fatture) as Fatture2
ON Fatture2.[Codice Progetto] = Progetti.[Codice Progetto];
Maybe I have found why it does not work: Fatture.[Codice Progetto] is probably linked to Progetti.[ID Progetto] (that is different from Progetti.[ID Progetto]). The field is set in this way:
hope this works for you
UPDATE Progetti
SET Progetti.Eroso = Fatture2.FatturaSum
FROM Progetti
Inner Join (SELECT sum(Fatture.Fattura) as FatturaSum
FROM Fatture) as Fatture2
ON Fatture2.[Codice Progetto] = Progetti.[Codice Progetto];

unable to update DB2 table

Can you please help me in updating DB2 table and is there a better way to update this huge table? Adv thxs.
UPDATE RT.ITEM IM SET
IM.ITEMNAME = GT.ITEM_D, IM.ITEMSIZE = GT.SIZE, IM.COLOR = GT.COL,
IM.ITEMINFO = GT.ITEM_I WHERE IM.RET = 14 AND IM.LAN = 10 and
IM.ITEMK IN ( SELECT GT.SN_N FROM GD.G_TEMP GT );
Trying to update a table(RT.ITEM) from another schema table(GD.G_TEMP) and getting below error msg:
[Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=GT.ITEM_D
Your code won't work. DB2 doesn't support explicit JOIN in UPDATE. But you can do what you want with a correlated subquery:
UPDATE RT.ITEM IM
SET (ITEMNAME, ITEMSIZE, COLOR, ITEMINFO) =
(SELECT GT.ITEM_D, GT.SIZE, GT.COL, GT.ITEM_I
FROM GD.G_TEMP GT
WHERE GT.SN_N = IM.ITEMK
FETCH FIRST 1 ROW ONLY
)
WHERE IM.RET = 14 AND IM.LAN = 10 AND
EXISTS (SELECT 1
FROM GD.G_TEMP GT
WHERE GT.SN_N = IM.ITEMK
);
Hi you can try with merge command, if distinct don't solve your problems in gd_temp table with multiple rows for single sn_n value then you will have to add more filters in subquery.
MERGE INTO RT.ITEM IM
USING
(SELECT DISTINCT
GT.ITEM_D,
GT.SIZE,
GT.COL,
GT.ITEM_I
FROM GD.G_TEMP gt
) gt on gt.sn_n=im.itemk AND IM.RET = 14 AND IM.LAN = 10
WHEN MATCHED THEN UPDATE
SET (im.ITEMNAME, im.ITEMSIZE, im.COLOR, im.ITEMINFO) = (GT.ITEM_D, GT.SIZE, GT.COL, GT.ITEM_I)

Update set From Select Error Syntax

Im trying to use Update set like above, i try different kind of syntaxes but no success
update
pc_discount
set
ceza_ucret_turu_id=pc_discount_ceza.CEZA_ALT_UCRET_KODU
from
(select CEZA_ALT_UCRET_KODU from pc_discount_ceza where CEZA_ALT_UCRET_KODU is not null) pdc
where
pc_discount.id=pdc.discount_id
ceza_ucret_turu_id is null
first one no success(sql command not finished properly)
MERGE INTO pc_discount pd
USING pc_discount_ceza pdc
ON pd.id = pdc.discount_id
AND pdc.CEZA_ALT_UCRET_KODU is not null
and pd.ceza_ucret_turu_id is null
WHEN MATCHED THEN
UPDATE
SET ceza_ucret_turu_id = pdc.CEZA_ALT_UCRET_KODU
no success again(missing ON keyword)
Is there any advice to run that kind of sql?
EDIT:
update
pc_discount
set
ceza_ucret_turu_id=pdc.CEZA_ALT_UCRET_KODU
from
(select CEZA_ALT_UCRET_KODU from pc_discount_ceza where CEZA_ALT_UCRET_KODU is not null) pdc
where
pc_discount.id=pdc.discount_id and
ceza_ucret_turu_id is null
still no change.
There are a couple of things I would change
there are two conditions, they need joining with an "AND" set should
use the table alias in the set
add the joining condition into the subquery
Try something like
update
pc_discount
set
ceza_ucret_turu_id = pcd.CEZA_ALT_UCRET_KODU
from
(
select
discount_id,
CEZA_ALT_UCRET_KODU
from
pc_discount_ceza
where
CEZA_ALT_UCRET_KODU is not null
) pcd
where
pc_discount.id = pcd.discount_id and
pc_discount.ceza_ucret_turu_id is null
Try this - I'm not sure you're using correct syntax for Oracle
UPDATE pc_discount pd
SET ceza_ucret_turu_id =
(SELECT
MAX(ceza_alt_ucret_kodu)
FROM
pc_discount_ceza pdc
WHERE pd.id = pdc.discount_id
)
WHERE pd.ceza_ucret_turu_id IS NULL
;
The above has been updated with your new business rule

How do I update multiple columns with a subquery in a single statement?

I am attempting to update a temp table from a source table:
UPDATE #DETAIL
SET EXCD_ID, CDOR_OR_AMT, CDOR_OR_VALUE
(SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA CDID
WHERE
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
)
FROM #DETAIL DTL
WHERE DTL.CDOR_OR_ID = 'XS'
Unfortunately it complains
Incorrect syntax near ',' (on the '(SELECT' line)
Incorrect syntax near 'FROM' (the second one)
After much trial and error I pooled some help at work and we came up with this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CONVERT(MONEY,CDID.CDID_ADDL_DATA),
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN
CMC_CDID_DATA CDID ON
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO
WHERE DTL.CDOR_OR_ID = 'XS'
AND CDID.CDID_TYPE = 'NDC'
Which sybase seems to accept.
You have to make the update like this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CDID.CDID_ADDL_DATA
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN (SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA ) CDID ON CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
WHERE DTL.CDOR_OR_ID = 'XS'
Check THIS ARTICLE for more info!
I just tried this out and it worked (on Oracle)
update dstTable T
set (T.field1, T.field2, T.field3) =
(select S.value1, S.value2, S.value3
from srcTable S
where S.key = T.Key);
This, unfortunately is Oracle specific syntax.
Caveat: Note that the update above has no where clause. It updates the entire table. If the subquery return no rows then the target fields are set to NULL. Also, it's an error if the subquery returns more than one row.