unable to update DB2 table - sql

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)

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

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.

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

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

How can be expressed this Access Query to SQL Server query

I have a database in access but recently moved to SQL server, and I have modified almost all queries but this one :
UPDATE Articulos_Auditoria
INNER JOIN Auditoria ON Articulos_Auditoria.CUD = Auditoria.CUD
SET Articulos_Auditoria.Cortado = 'True'
WHERE
(((CAST([Fecha] AS DATE)) = CAST(#Fecha AS DATE))
AND ((Auditoria.Terminal) = #term))
I am trying to convert it to SQL Server (since I changed DateValue to a CAST) but intellisense gives me a syntax error near 'INNER'
Can anyone give me some insight?
You have two errors:
You need to define the SET after the UPDATE TableName
You need to define a FROM clause
UPDATE Articulos_Auditoria
SET Cortado = 'True'
FROM Articulos_Auditoria
INNER JOIN Auditoria
ON Articulos_Auditoria.CUD = Auditoria.CUD
WHERE CAST([Fecha] as date)=CAST(#Fecha as date)
AND Auditoria.Terminal=#term
Use the from clause and table aliases:
UPDATE aa
SET Cortado = 'True'
FROM Articulos_Auditoria aa INNER JOIN
Auditoria a
ON aa.CUD = a.CUD
WHERE CAST([Fecha] as date) = CAST(#Fecha as date) AND (a.Terminal = #term)

Update From clause in SQL Server CE doesn't work , any Solutions?

I've this SQL statement:
UPDATE Movement_Item_Lots
SET Batch_Code = (SELECT WHSS.Batch_Code
FROM WH_Stock_Serials AS WHSS
WHERE WHSS.Item_Code = Movement_Item_Lots.Item_Code
AND WHSS.From_Distribution_Code = Movement_Item_Lots.Distribution_Code
)
it returns :
There was an error parsing the query.
[ Token line number = 2,Token line offset = 19,Token in error = SELECT ]
I know this is common issue in SQL Server CE that it can't do update from, any workaround for this issue ?
Change to sqlite, if possible, this sql would work... If not possible, you can always divide the statement in your program:
var <- SELECT WHSS.Batch_Code...
UPDATE .. SET Batch_Code = var