Update error column parameter does not exist - sql

Trying to execute this query I've this error:
Update failed. 3810: Column/parameter 'edw_workarea.A.A' does not exist.
I'm using Teradata database.
UPDATE A
FROM EDW_WORKAREA.bs A,
(
SELECT stg.ccir_ind_id,
stg.orig_ccir_id
FROM EDW_WORKAREA.se stg
INNER JOIN best_svc bc
ON stg.ccir_ind_id = bc.ccir_ind_id
WHERE stg.deliverability_score < '6'
AND stg.ccir_ind_id IN (SELECT stg.ccir_ind_id
FROM EDW_WORKAREA.se stg
WHERE stg.orig_ccir_id IS NULL
OR stg.orig_ccir_id = ''
GROUP BY
stg.ccir_ind_id
HAVING COUNT(*) = 1)
) t
SET A.orig_ccir_id = t.orig_ccir_id
WHERE A.ccir_ind_id = t.ccir_ind_id;
All the tables and columns exist in database. And subquery in t was executing successfully alone.
Can any one point me where the error is ?

On Teradata, you shouldn't qualify your columns in the SET clause, so change your SQL to something like:
update EDW_WORKAREA.bs
from (
select ...
) t
set orig_ccir_id = t.orig_ccir_id
where EDW_WORKAREA.bs.ccir_ind_id = t.ccir_ind_id;

Related

Cannot enclose a query with an outer query in Firebird 2.5

My inner query is returning results but I get this error
SQL Error [336397221] [42000]: Dynamic SQL Error; SQL error code =
-104; Invalid command; column NOME was specified multiple times for derived table EX [SQLState:42000, ISC error code:336397221]
Here is my query
select * from ( --this gives error
SELECT t.placa, m.nome, GA.ID_AUDESP, GA.NOME AS GRUPO_AUDESP,
T.VALOR as valor,
T.DT_AQUISICAO,
PS.NOME
FROM PATRIMONIO_TOMBAMENTO T
LEFT JOIN PATRIMONIO_GRUPO_AUDESP GA ON GA.ID_GRUPO_AUDESP = T.ID_GRUPO_AUDESP
LEFT JOIN ESTOQUE_MATERIAL M ON M.ID_MATERIAL = T.ID_MATERIAL
LEFT JOIN PATRIMONIO_SETOR PS ON (T.ID_SETOR = PS.ID_SETOR)
WHERE T.ID_ORGAO = '030000'
AND (T.SITUACAO IN('A') or ( T.SITUACAO = 'B' AND T.DT_BAIXA >'2022-01-31'))
AND (T.DT_REATIVADO IS NULL OR T.DT_REATIVADO<= '2022-01-31' or (T.DT_BAIXA >'2022-01-31'))
AND T.dt_cadastro <= '2022-01-31'
) ex
Your derived table has two columns with the name/alias NOME (m.nome and PS.NOME), and as the error indicates, this is not allowed as a name should occur only once. You need to alias one or both columns so they have unique names, or exclude one of them if it would have the same value. The same was already done for the third occurrence of NOME, GA.NOME.
For example:
select * from (
SELECT t.placa,
m.nome as m_nome,
GA.ID_AUDESP,
GA.NOME AS GRUPO_AUDESP,
T.VALOR as valor,
T.DT_AQUISICAO,
PS.NOME as ps_nome
FROM PATRIMONIO_TOMBAMENTO T
-- ...

My UPDATE statement with WHERE EXISTS does not limit to the SELECT statement results

I have data in a temporary table and I am checking for duplicates in two other tables. I want to set a flag on the temp table (SET DupFlag = TRUE) for all duplicates found. My SELECT statement works perfectly, returning only the 48 duplicates that I entered to test with. But when I add UPDATE and WHERE EXISTS, every record in idTempImport2 is set to TRUE instead of just the 48 records returned from the SELECT statement. Is my syntax wrong? It looks like this:
UPDATE idTempImport2 as tmp2 SET DupFlag = TRUE
WHERE EXISTS
(SELECT * FROM idTempImport2 tmp2
LEFT JOIN (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2
FROM idMain AS im
INNER JOIN idNotes AS idn ON im.aID = idn.MainID
WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31") AS qry
ON qry.idDate = tmp2.idDate AND qry.UserID = tmp2.UserID AND qry.ActionID = tmp2.ActionID AND qry.Epic1 = clng(tmp2.Epic1) AND qry.Epic2 = clng(tmp2.Epic2)
WHERE qry.Epic1 <> NULL);
I think the ultimate issue is that you want a correlated subquery. As written, the subquery has no connection to the outer query, so it is likely that at least one row meets the condition. So, everything gets updated.
I think you intend:
UPDATE idTempImport2 as tmp2
SET DupFlag = TRUE
WHERE EXISTS (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2
FROM idMain AS im INNER JOIN
idNotes AS idn
ON im.aID = idn.MainID
WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31" AND
im.idDate = tmp2.idDate AND im.UserID = tmp2.UserID AND
im.ActionID = tmp2.ActionID AND
?.Epic1 = clng(tmp2.Epic1) AND ?.Epic2 = clng(tmp2.Epic2)
);

Teradata 3706, expected something between DISTINCT and LEFT keyword

I am here trying to use UPDATE query with JOIN and SELECT, But I am facing 3706 error.
here is the error line -
UPDATE A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A2
FROM
(SELECT A.REP_CD, A.GEO_CD,A.MONTH_ID, B.IC_PRD AS IC_PAYOUT_FLAG,
COALESCE(CASE
WHEN UPPER(B.IC_PRD) = 'SEMESTERLY' THEN A.CURR_SEMESTER_FLAG
WHEN UPPER(B.IC_PRD) = 'QUARTERLY' THEN A. CURR_QUARTER_FLAG
ELSE '0'
END,'0') IC_PAYOUT_FLAG
FROM
A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A,
(
SELECT DISTINCT LEFT(B. REP_CD, POSITION('-' IN B.REP_CD)-1) AS REP_CD,C.GEO_CD,IC_PRD FROM
A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD A, A0619IL3549_D00_IC_MAIN.ICDM_DIM_REP B,A0619IL3549_D00_IC_MAIN.ICDM_DIM_GEO_HIER C
WHERE
A.REP_SK=B.REP_SK
AND A.GEO_SK=C.GEO_SK
AND A.RUN_ID = (SELECT MAX(RUN_ID) FROM A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD)
) B
WHERE
A.REP_CD = B.REP_CD
AND
A.GEO_CD = B.GEO_CD)A1
SET A2.IC_PAYOUT_FALG = A1.IC_PAYOUT_FLAG
WHERE
A1.REP_CD = A2.REP_CD
AND
A1.MONTH_ID = A2.MONTH_ID
AND
A.GEO_CD = A2.GEO_CD
LEFT is not a valid Teradata function before TD15.10, it's ODBC SQL, which is automatically translated, but only for SELECT.
Use valid syntax instead:
SUBSTRING(B. REP_CD FROM 1 FOR POSITION('-' IN B.REP_CD)-1)

Updating row of one table using rows of another table based on specific conditions

I want to update OFFICE_ID,OFFICE_TYPE of FA_SUBLEDGER_MST table, by using OFFICE_ID,OFFICE_TYPE from EMPLOYEE_MST table based on following conditions:
EMPLOYEE_MST.SL_CODE=FA_SUBLEDGER_MST.SL_CODE
EMPLOYEE_MST.OFFICE_ID<>SL.OFFICE_ID
OR EMPLOYEE_MST.OFFICE_TYPE<>SL.OFFICE_TYPE
AND EMPLOYEE_MST.OFFICE_TYPE!='DHB'.
I tried this query:
UPDATE FA_SUBLEDGER_MST sl
SET
(
sl.OFFICE_ID,sl.OFFICE_TYPE
)
=
(SELECT emp.OFFICE_ID,
emp.OFFICE_TYPE
FROM EMPLOYEE_MST emp
WHERE emp.OFFICE_ID<>sl.OFFICE_ID
OR emp.OFFICE_TYPE<>sl.OFFICE_TYPE
AND sl.SL_CODE = emp.SL_CODE
AND emp.OFFICE_TYPE!='DHB'
)
WHERE sl.STATUS = 'A'
AND EXISTS
(SELECT 1
FROM EMPLOYEE_MST emp
WHERE emp.OFFICE_ID<>sl.OFFICE_ID
OR emp.OFFICE_TYPE<>sl.OFFICE_TYPE
AND emp.SL_CODE=sl.SL_CODE
AND emp.OFFICE_TYPE!='DHB'
);
And this:
UPDATE FA_SUBLEDGER_MST
SET
(
OFFICE_ID,
OFFICE_TYPE
)
=
(SELECT OFFICE_ID,
OFFICE_TYPE
FROM EMPLOYEE_MST
WHERE FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID
OR FA_SUBLEDGER_MST.OFFICE_TYPE! =EMPLOYEE_MST.OFFICE_TYPE
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
) ;
But both caused this error:
SQL Error: ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row
I also tried:
UPDATE
(SELECT em.OFFICE_ID emoffid,
SL.OFFICE_ID sloffid,
em.OFFICE_TYPE emofftype,
SL.OFFICE_TYPE slemofftype,
SL.STATUS
FROM EMPLOYEE_MST em
JOIN FA_SUBLEDGER_MST SL
ON em.SL_CODE =SL.SL_CODE
WHERE em.OFFICE_ID<>SL.OFFICE_ID
OR em.OFFICE_TYPE <>SL.OFFICE_TYPE
AND em.OFFICE_TYPE!='DHB'
AND SL.STATUS ='A'
) t
SET t.sloffid =t.emoffid
WHERE t.emoffid<>t.sloffid
OR t.emofftype <>t.slemofftype
AND t.emofftype!='DHB'
AND t.STATUS ='A';
This caused an error:
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
Both the tables contain more than 100,000 records and OFFICE_ID can be 1,2,3,4,5 and OFFICE_TYPE can be 'DE','DI','HO', hence they repeat frequently.
I need an Oracle query to do so. Can't use a procedure.
Would be thankful for your help.
TRY THIS:
WHERE (FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID AND
FA_SUBLEDGER_MST.OFFICE_TYPE !=EMPLOYEE_MST.OFFICE_TYPE) --AND will be converted to OR by SQL Engine
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
AND sl.STATUS = 'A'
Try
UPDATE FA_SUBLEDGER_MST
SET sl.OFFICE_ID = em.OFFICE_ID
,sl.OFFICE_TYPE = em.OFFICE_TYPE
FROM FA_SUBLEDGER_MST sl
INNER JOIN EMPLOYEE_MST em ON em.SL_CODE =SL.SL_CODE
WHERE em.OFFICE_ID<>SL.OFFICE_ID
OR em.OFFICE_TYPE <>SL.OFFICE_TYPE
AND em.OFFICE_TYPE!='DHB'
AND SL.STATUS ='A'
try this code:
UPDATE FA_SUBLEDGER_MST
SET
(
OFFICE_ID,
OFFICE_TYPE
)
=
(SELECT OFFICE_ID,
OFFICE_TYPE
FROM EMPLOYEE_MST
WHERE .......
)
WHERE (FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID AND
FA_SUBLEDGER_MST.OFFICE_TYPE !=EMPLOYEE_MST.OFFICE_TYPE)
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
AND sl.STATUS = 'A' ;
Finally found a solution:
merge into FA_SUBLEDGER_MST s1
using (
select SL_CODE, OFFICE_ID, OFFICE_TYPE
from EMPLOYEE_MST
where OFFICE_TYPE != 'DHB'
) emp
on (s1.SL_CODE = emp.SL_CODE)
when matched then update set
s1.OFFICE_ID = emp.OFFICE_ID,
s1.OFFICE_TYPE = emp.OFFICE_TYPE
where sl.STATUS = 'A'
and (s1.OFFICE_ID <> emp.OFFICE_ID
or s1.OFFICE_TYPE <> emp.OFFICE_TYPE)
;
Orignal Thread: Updating row of one table using rows of another table based on specific conditions on https://dba.stackexchange.com/

Why does a CTE in SQL Server execute the INNER JOIN when no conditions are met?

I have table mse that have all rows StatusId = 1. But in query like this INNER JOINED VIEW is executed regardless of value of column StatusId. How to prevent it?
WITH cte201401291517 AS
(
SELECT
'QuantityOutPerShift' = SUM([vsqo].[QuantityOutPerShift])
, [mse].[ShiftGroup]
, [mse].[Station]
, [vsqo].[Shift]
FROM
[dbo].[mse] AS mse
INNER JOIN
[dbo].[vmsqo] AS vsqo ON [mse].[Station] = [vsqo].[FromStation]
AND ( [mse].[ShiftGroup] = [vsqo].[ShiftGroup]
OR [mse].[ShiftGroup] = 'ALL') -- order is important!
WHERE
[mse].[StatusId] = 3
GROUP BY
[mse].[ShiftGroup]
, [mse].[Station]
, [vsqo].[Shift])
UPDATE
[dbo].[mse]
SET
[dbo].[mse].[QuantityOutPerShift] = [cte].[QuantityOutPerShift]
, [dbo].[mse].[ShiftCurrent] = [cte].[Shift]
--OUTPUT INSERTED.*
FROM
cte201401291517 AS cte
WHERE
[dbo].[mse].[Station] = [cte].[Station]
AND ( [dbo].[mse].[ShiftGroup] = [cte].[ShiftGroup]
OR [dbo].[mse].[ShiftGroup] = 'ALL' ) -- order is important!
AND [dbo].[mse].[StatusId] = 3;
I can't do this without CTE because of fact that I'm updating table with SUM that cannot be used in UPDATE statement.
I'm using SQL Server 2005