ORA-00904: invalid identifier but table and column names are correct? - sql

I have the below SQL query but it gives the error message ORA-00904: "KUST_ADR"."KU_NR": invalid identifier even though those are the correct table and column names. What else could be the cause?
update auf_adr
set email = (select k.ku_email
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
)
where auf_adr.adr_art = 2 and
exists (select 1
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
);

There is a "and" missing after each of the "where" clause line, this could be the issue.
where auf_adr.auf_nr = k.auf_nr AND

Related

How to select twice the same column on join query?

How Can I to count the gender when is M or F, somehting like
SELECT count(N.gender)
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1 and N.gender="M"
SELECT countif(N.gender = 'M') as M, countif(N.gender = 'F') as F
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1
Since you did not describe the structure of your table and use Spanish-looking identifiers, I will use a clearer example with my own schema:
SELECT
SUM( CASE WHEN Sex = 'M' THEN 1 ELSE 0 END ) AS M,
SUM( CASE WHEN Sex = 'F' THEN 1 ELSE 0 END ) AS F
FROM People
WHERE People.Dept = 5

Convert to linq sql query error

I have a query according to the :
SELECT
tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title AS EvaluationCategory,
CASE tType WHEN 1 THEN N'2'
ELSE N'1' END ProcessType,
tblDefinition.Description, tblDefinition.TemplateID, 1 Destinition
FROM tblTemplate INNER JOIN
tblEvaluationFormCategory ON tblTemplate.CategoryID = tblEvaluationFormCategory.ID INNER JOIN
tblEFQMAlternative INNER JOIN
tblDefinition ON ([tblEFQMAlternative].[TopsisID] = [tblDefinition].[TopsisID] AND [tblEFQMAlternative].[CustomerID] = [tblDefinition].[CustomerID]) INNER JOIN
tblGroupMembers ON ([tblDefinition].[TopsisID] = [tblGroupMembers].[TopsisID]
AND tblDefinition.CustomerID =
tblGroupMembers.CustomerID) ON tblTemplate.TemplateID = tblDefinition.TemplateID
INNER JOIN
tblTerm ON tblDefinition.TermGrant = tblTerm.Title
WHERE (tblTerm.ID = 20) AND (tblGroupMembers.UserID = 558)
GROUP BY tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title,
tblDefinition.Description, tblDefinition.TemplateID
when i convert to linq, then error to :
Field [([tblDefinition].[TopsisID] = [tblGroupMembers]].[[TopsisID] AND tblDefinition.CustomerID = tblGroupMembers.CustomerID) ON tblTemplate.TemplateID] not found in the current Data Context.
What should I do?

SQL CASE Clauses with variable

I get this error:
Incorrect syntax near '='.
Code:
WHERE
T.[ID] = -9769
AND TNS.Active = 1
AND CASE
WHEN T.[StatusID] IN (1,6)
THEN (T.[AuditUser_ID] = 2 AND TNX.Actor = 2)
END
WHERE
T.[ID] = -9769
AND TNS.Active = 1
AND 1 = CASE
WHEN T.[StatusID] IN (1,6)
and (T.[AuditUser_ID] = 2 AND TNX.Actor = 2) then 1 else 0
END

update not working

UPDATE dwh.product_in_offer AS t
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
WHERE create_id = 0;
ERROR:Syntax error near INNER, offset 386 "..ND t.OFFER_KEY=s.OFFER_KEY) -->INNER<--"
Any idea ??
The INNER JOIN clause has to go before the SET clause.
UPDATE dwh.product_in_offer AS t
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
WHERE create_id = 0;

SSIS SSQL Task error

I am trying to update a table using a SQL task in SSIS and I'm getting an error: The multi-part identifier "a.SourceSystemKey" could not be bound.
Update BMR_STAGE.dbo.STG_AL_VSAccountStatuses
set a.SourceSystemKey = b.SourceSystemKey
,a.SourceSystem = b.SourceSystem
,a.NLCompany = b.NLCompany
,a.AccountStatus = b.AccountStatus
,a.Description = b.Description
,a.InsertAuditKey = b.InsertAuditKey
,a.UpdateAuditKey = b.UpdateAuditKey
,a.ChangeDate = b.ChangeDate
from BMR_STAGE.dbo.STG_AL_VSAccountStatuses a, BMR_STAGE.dbo.TMP_STG_AL_VSAccountStatuses b
where a.ID =b.ID
That error indicates either that there is no field called a.SourceSystemKey or that the alias a is not recognized in that scope. Try using the explicit join syntax.
Update a
set
a.SourceSystemKey = b.SourceSystemKey
,a.SourceSystem = b.SourceSystem
,a.NLCompany = b.NLCompany
,a.AccountStatus = b.AccountStatus
,a.Description = b.Description
,a.InsertAuditKey = b.InsertAuditKey
,a.UpdateAuditKey = b.UpdateAuditKey
,a.ChangeDate = b.ChangeDate
from BMR_STAGE.dbo.STG_AL_VSAccountStatuses a inner join
BMR_STAGE.dbo.TMP_STG_AL_VSAccountStatuses b on a.ID = b.ID