ORA-38104 when trying to update my table using merge - sql

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.

Related

Redshift - ERROR: Target table must be part of an equijoin predicate

I am trying to make an update on a temporal table I created in Redshift. The code I am trying to run goes like this:
UPDATE #new_emp
SET rpt_to_emp_id = CAST(ht.se_value AS INTEGER),
rpt_to_extrnl_email = ht.extrnl_email_addr,
rpt_to_fst_nm = ht.first_nm,
rpt_to_lst_nm = ht.last_nm,
rpt_to_mdl_init = ht.mdl_nm,
rpt_to_nm = ht.full_nm,
rpt_to_ssn = CAST(ht.ssn AS INTEGER),
FROM #new_emp,
(SELECT DISTINCT t.se_value,h.first_nm,h.last_nm,
h.mdl_nm,h.full_nm,h.ssn,h.extrnl_email_addr
FROM spec_hr.dtbl_translate_codes_dw t, spec_hr.emp_hron h
WHERE t.inf_name = 'system'
AND t.fld_name = 'HRONDirector'
AND h.foreign_emp_id = t.se_value
) ht
WHERE #new_emp.foreign_emp_id <> ht.se_value
AND (#new_emp.emp_status_cd <> 'T'
AND (#new_emp.ult_rpt_emp_id = #new_emp.foreign_emp_id
OR #new_emp.ult_rpt_emp_id = #new_emp.psoft_id
OR #new_emp.ult_rpt_emp_id IS NULL));
I've tried both with and without specyfing the updated table from the FROM command. But it keeps throwing me this error:
ERROR: Target table must be part of an equijoin predicate
Any ideas why is this failing? Thank you!
Redshift needs an equality join condition to know what value to update and with which values. Your join condition is "#new_emp.foreign_emp_id <> ht.se_value" which is an inequality or Redshift speak - this is not "an equijoin predicate". You have a SET of "rpt_to_lst_nm = ht.last_nm" but if the only join condition is an inequality then which value of last_nm is Redshift putting in the table?
To put it the other way around - you need to tell Redshift exactly which rows of the target table are receiving which values (equijoin). The join condition you have doesn't meet this requirement.

ORA-02070: database INVWARE does not support in this context

Background:
I am wanting to run this merge inside a procedure on a schedule. I have to insert new data into the sql database table and if the data exist, I am wanting to update the quantities.
Problem:
I am trying to do a merge from an Oracle database to a sql database and getting an error (see the title of this question). I have tried using the merge with the same sql script used to create the view and it returned the same error.
Question:
Is the problem something in my code (see below)?
MERGE INTO "receipt_details"#invware d
USING (
SELECT *
FROM raf_po_receiving_details_v
WHERE last_update_date >= '1-AUG-2013' ) s
ON ( "po_header_id" = s.po_header_id
and "po_line_id" = s.po_line_id )
WHEN MATCHED THEN UPDATE
SET "purchased_qty" = s.purchased_qty,
"qty_received" = s.qty_received,
"balance_remaining" = s.balance_remaining,
"qty_billed" = s.qty_billed
WHEN NOT MATCHED THEN INSERT ( "warehouse_code", "po_number", "po_header_id",
"vendor_name", "line_num",
"item_code", "purchased_qty", "qty_received",
"rcv_by", "balance_remaining", "qty_billed",
"closed_code", "rec_date", "need_by_date", "po_line_id" )
VALUES (s.warehouse_code, s.po_number, s.po_header_id, s.vendor_name,
s.line_num, s.item_code, s.purchased_qty, s.qty_received, s.rcv_by,
s.balance_remaining, s.qty_billed, s.closed_code, s.rec_date, s.need_by_date, po_line_id);

How to update columns in a table joined with other tables?

I want to update two columns of a table with reference of other tables. While executing the script its showing error.
Error: Error starting at line 1 in command:
UPDATE wb_costing_work_items,
sa_sales_documents,
sa_sales_document_items
SET cwi_price_per_hour = sdi_price,
cwi_amount = sdi_price * cwi_hours
WHERE cwi_lo_id = sad_lo_id
AND sdi_sad_id = sad_id
AND sdi_wit_id = cwi_wit_id
AND cwi_id = 1650833
Error at Command Line:1 Column:28 Error report: SQL Error: ORA-00971:
missing SET keyword
00971. 00000 - "missing SET keyword"
SQL STATEMENT
UPDATE wb_costing_work_items cwi,
sa_sales_documents sad,
sa_sales_document_items sdi
SET cwi.cwi_price_per_hour = sdi.sdi_price,
cwi.cwi_amount = sdi.sdi_price * cwi.cwi_hours
WHERE cwi.cwi_lo_id = sad.sad_lo_id
AND sdi.sdi_sad_id = sad.sad_id
AND sdi.sdi_wit_id = cwi.cwi_wit_id
AND cwi.cwi_id = 1650855
This should definitely work.
UPDATE (SELECT cwi_price_per_hour,
sdi_price,
cwi_amount,
sdi_price,
cwi_hours
FROM wb_costing_work_items,
sa_sales_documents,
sa_sales_document_items
WHERE cwi_lo_id = sad_lo_id
AND sdi_sad_id = sad_id
AND sdi_wit_id = cwi_wit_id
AND cwi_id = 1650833)
SET cwi_price_per_hour = sdi_price, cwi_amount = sdi_price * cwi_hours
Please alias the tables used and prefix columns so one can easily read your query.
Something like this maybe.
Note that I had to use some wild guesses about which column belongs to which table as you have not included any information about how your tables are define.
So most probably I didn't get it right and you will need to adjust the query to your actual table structure.
merge into wb_costing_work_items
using
(
select cwi.pk_column, -- don't know what the PK of the wb_costing_work_items is due to lack of information
sdi.sdi_price, -- don't know if this column really belong to this table
sdi.sdi_price * cwi.cwi_hours as total-- again unsure about column names due to lack of information
FROM wb_costing_work_items cwi
JOIN sa_sales_documents sad ON sad.sad_lo_id = cwi.cwi_lo_id -- not sure about these, due to lack of information
JOIN sa_sales_document_items sdi
ON sdi.sad_id = sad.sdi_sad_id -- not sure about these, due to lack of information
AND sdi.sdi_wit_id = cwi.cwi_wit_id -- not sure about these, due to lack of information
) t ON (t.pk_column = w.pk_column) -- not sure about this, due to lack of information
when matched then
update
set cwi_price_per_hour = t.sdi_price,
cwi_amount = t.total;

Oracle customized merge

We have two tables one is source and another one is target. The source table gets updated/refresh with external data source and we have a PL/SQL block which runs daily and updates/inserts the target table rows using Oracle's merge function.
Now we have a situation where a column (email) in the target table could get updated by some external source. Consequently, this email is being updated/overwritten by email of source table with the above mentioned PL/SQL's merge function.
Is there something in the merge function so that it only updates the rows in the target table if things other than the email have been changed. I am happy to add new column(s) into the source and target table if needed.
create or replace
PROCEDURE INSERT_UPDATE_TARGET AS
BEGIN
merge into INTEGRATION_TARGET t
using v_merge vm
on (t.person_num = vm.person_num)
when matched then update set T.YEAR_START = VM.YEAR_START,
T.SEMESTER_START = VM.SEMESTER_START,
T.SATAC_DATABASE_NAME = VM.SATAC_DATABASE_NAME,
T.STUDY_LEVEL = VM.STUDY_LEVEL,
T.REF_NUM = VM.REF_NUM,
T.SEX = VM.SEX,
T.DATE_OF_BIRTH = VM.DATE_OF_BIRTH,
T.DATE_RECEIVED = VM.DATE_RECEIVED,
T.TITLE = VM.TITLE,
T.FAMILY_NAME = VM.FAMILY_NAME,
T.GIVEN_NAME_1 = VM.GIVEN_NAME_1,
T.STREET_NAME = VM.STREET_NAME,
T.STREET_NAME_2 = VM.STREET_NAME_2,
T.STREET_NAME_3 = VM.STREET_NAME_3,
T.POSTAL_STATE = VM.POSTAL_STATE,
T.POSTAL_TOWN = VM.POSTAL_TOWN,
T.POSTAL_POSTCODE = VM.POSTAL_POSTCODE,
T.COUNTRY = VM.COUNTRY,
T.MOBILE = VM.MOBILE,
T.EMAIL = VM.EMAIL
when not matched then insert (T.YEAR_START,
T.SEMESTER_START,
T.SATAC_DATABASE_NAME,
T.STUDY_LEVEL,
T.REF_NUM,
T.PERSON_NUM,
T.SEX,
T.DATE_OF_BIRTH,
T.DATE_RECEIVED,
T.TITLE,
T.FAMILY_NAME,
T.GIVEN_NAME_1,
T.STREET_NAME,
T.STREET_NAME_2,
T.STREET_NAME_3,
T.POSTAL_STATE,
T.POSTAL_TOWN,
T.POSTAL_POSTCODE,
T.COUNTRY,
T.MOBILE,
T.EMAIL
)
values(VM.YEAR_START,
VM.SEMESTER_START,
VM.SATAC_DATABASE_NAME,
VM.STUDY_LEVEL,
VM.REF_NUM,
VM.PERSON_NUM,
VM.SEX,
VM.DATE_OF_BIRTH,
VM.DATE_RECEIVED,
VM.TITLE,
VM.FAMILY_NAME,
VM.GIVEN_NAME_1,
VM.STREET_NAME,
VM.STREET_NAME_2,
VM.STREET_NAME_3,
VM.POSTAL_STATE,
VM.POSTAL_TOWN,
VM.POSTAL_POSTCODE,
VM.COUNTRY,
VM.MOBILE,
VM.EMAIL
);
END;
Thanks
Add a where clause to your update statement

sqlite3 UPDATE generating nulls

I'm trying to transition from MySQL to SQLIte3 and running into an update problem. I'm using SQLite 3.6.20 on redhat.
My first line of code behaves normally
update atv_covar set noncomp= 2;
All values for noncomp (in the rightmost column) are appropriately set to 2.
select * from atv_covar;
A5202|S182|2
A5202|S183|2
A5202|S184|2
It is the second line of code that gives me problems:
update atv_covar
set noncomp= (select 1 from f4003 where
atv_covar.study = f4003.study and
atv_covar.rpid = f4003.rpid and
(rsoffrx="81" or rsoffrx="77"));
It runs without generating errors and appropriately sets atv_covar.noncomp to 1 where it matches the SELECT statement. The problem is that it changes atv_covar.noncomp for the non-matching rows to null, where I want it to keep them as 2.
select * from atv_covar;
A5202|S182|
A5202|S183|1
A5202|S184|
Any help would be welcome.
#Dan, the problem with your query is not specific to SQLite; you are updating all rows of atv_covar, but not all of them have correspondence in f4003, so these default to NULL. You should filter the update or provide a default value.
The following statement sets 1 only to the rows that macth the filtering condition:
UPDATE atv_covar
SET noncomp = 1
WHERE EXISTS (
SELECT 'x'
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
);
The following statement sets 1 or 2 for all rows of noncomp, depending on the filtering match (use this instead of two updates):
UPDATE atv_covar
SET noncomp = COALESCE((
SELECT 1
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
), 2);