Tera Data: Missing/Invalid SQL statement'E(3810) - sql

I have this simple merge statement but it failed when running.
Any advice is appreciated.
MERGE INTO HP.SampleAll as A
USING (
select ALIGNED
from HP.Sample2
) as B
ON (A.md_nbr = B.md_nbr)
WHEN MATCHED THEN UPDATE
SET ALIGNED = A.ALIGNED ;

Error 3810 says that Column does not exist.
md_nbr dont exist in your subquery B - as you don't select it.
Maybe a solution is:
MERGE INTO HP.SampleAll as A
USING (
select ALIGNED, md_nbr
from HP.Sample2
) as B
ON (A.md_nbr = B.md_nbr)
WHEN MATCHED THEN UPDATE
SET ALIGNED = A.ALIGNED ;
or just USING HP.Sample2 as B

Related

Error while Trying to update an Oracle nested table

I am trying to update some fields in oracle nested table, so I can insert some values, the problem is I am unable to retrieve the results as nested table. The following is the query I am trying to run:
update utilisation_obj
set listerefaccess = (
select ref(a)
from refaccessoireimb a
where a.refaccessoire.accessoire in ('ballon', 'barre')
)
where deref(utilisation_obj.REFTITREDENUM).titreDeNumero = 'Les Zoupalas' and
deref(utilisation_obj.REFUTILISATEUR).nom = 'Louis';
The error is the following:
inconsistent datatypes: expected CIRQUEOR.LISTEREFACCESS_T got : REF CIRQUEOR.REFACCESSOIRE_T
I found this solution but I was hoping for a more dynamic solution since this one would force me to know how many rows I expect to have
INSERT INTO Utilisation_Obj
Select Ref(N), Ref(P), ListeRefAccess_T(RefAccessoire_T(ref(A1)),RefAccessoire_T(ref(A2)))
From Numeros_Obj N, Personnel_Obj P, Accessoire_Obj A1,Accessoire_Obj A2
Where N.TitreDeNumero = 'Les Zoupalas' and P.Nom = 'Louis' and
A1.accessoire = 'ballon' and A2.accessoire = 'barre';

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.

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)

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);

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);