Oracle update statement multiple tables - oracle

I am using Oracle (Still a little new to it) and each time i run the update statement below i get the following error message.
SQL Error: ORA-00904: "CH"."CONTRACT_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Here is the query:
UPDATE wr00262_catalogue_201310 ct SET
ct.PORTFOLIO_ID = (SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID)
WHERE ct.NPC in (SELECT ctl.NPC
FROM wr00262_contract_line ctl
WHERE ctl.CONTRACT_ID = ch.CONTRACT_ID);
I think i may need a join but not quite sure where or how. The contract_header table does have a column called CONTRACT_ID.

This is a scoping issue. The columns in one sub-query are not visible to any other sub-query. So try something like this:
UPDATE wr00262_catalogue_201310 ct SET
PORTFOLIO_ID = (
SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
)
WHERE ct.NPC in (
SELECT ctl.NPC
FROM WR00262_CONTRACT_HEADER ch
join wr00262_contract_line ctl
on ctl.CONTRACT_ID = ch.CONTRACT_ID
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
);

Related

While updating a column using merge I am getting Error: ORA-38104

I am trying to update BUNDLE_NAME from below table,
My Query is :
MERGE INTO SOURCE S USING
(SELECT 'MANIFEST_ID' KEY,'CART' BUNDLE_NAME FROM DUAL) D
ON(S.KEY = D.KEY AND S.BUNDLE_NAME = D.BUNDLE_NAME)
WHEN MATCHED THEN UPDATE
SET BUNDLE_NAME = 'EPI';
but it gives me the following error:
Error report -
SQL Error: ORA-38104: Columns referenced in the ON Clause cannot be updated: "S"."BUNDLE_NAME"
38104. 00000 - "Columns referenced in the ON Clause cannot be updated: %s"
*Cause: LHS of UPDATE SET contains the columns referenced in the ON Clause**
--My requirement is that I want to update using merge statement only--
You can't have bundle_id in the on clause if you're trying to update it. As it's a fixed value, you can have it in a where clause instead:
MERGE INTO SOURCE S
USING (SELECT 'MANIFEST_ID' KEY FROM DUAL) D
ON (S.KEY = D.KEY)
WHEN MATCHED THEN UPDATE
SET BUNDLE_NAME = 'EPI'
WHERE S.BUNDLE_NAME = 'CART';
But you don't need a merge, you can do a simple update:
UPDATE SOURCE S
SET S.BUNDLE_NAME = 'EPI'
WHERE S.KEY = 'MANIFEST_ID'
AND S.BUNDLE_NAME = 'CART';
There are a few possible workarounds which can be used to outsmart the parser, at least until Oracle 18c. One of them is to wrap your predicate in a row value expression predicate using an additional dummy column:
MERGE INTO SOURCE S USING
(SELECT 'MANIFEST_ID' KEY,'CART' BUNDLE_NAME FROM DUAL) D
ON(S.KEY = D.KEY AND (S.BUNDLE_NAME, 'dummy') = ((D.BUNDLE_NAME, 'dummy')))
WHEN MATCHED THEN UPDATE
SET BUNDLE_NAME = 'EPI';

Missing keyword - inner join with where

post_code is a primary key in the location table and foreign key in job. I am trying to join them, could anyone help.
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location job.post_code ON location.post_code AND country_name_location = 'France';
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Your ON is in the wrong position:
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location ON job.post_code = location.post_code AND country_name_location = 'France';
There is an error in your syntax. The ON keyword is meant to come after you specify the table you are joining with. So the correct query would be:
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location ON job.post_code = location.post_code AND country_name_location = 'France';

00904. 00000 - "%s: invalid identifier" [duplicate]

This question already has an answer here:
Invalid Identifier SQL
(1 answer)
Closed 5 years ago.
Going crazy with this code. Can Anybody put me on the right track? I am using SQL Develope 3.1.07. Thanks.
SELECT
PHA.PO_HEADER_ID,
PRLA.ATTRIBUTE1 As "REQUISITION_NUMBER",
PHA.SEGMENT1 As PO_NUMBER,
DECODE (nvl(REG.DOCUMENT_TYPE, '-'),
'APW', 'Agreement for Performance of Work',
'APWEMER', 'Agreement for Performance of Work - Emergency',
'CONS', 'Consultant',
'CONCEMER', 'Consultant - Emergency',
'DFC', 'Direct Financial Contribution',
'FELLOW', 'Fellowship',
'GENEXT', 'General External Services',
'IMPRESTCC', 'Imprest/Credit Card',
'IS', 'Internal Services',
'LOA', 'Letter of Agreement',
'TSA', 'Technical Service Agreement') as DOCUMENT_TYPE,
HAOU.NAME As BUDGET_CENTRE,
PHA.CREATION_DATE As PO_CREATION_DATE,
PHA.AUTHORIZATION_STATUS,
PHA.APPROVED_DATE,
PHA.COMMENTS,
PHA.CLOSED_DATE,
PHA.CLOSED_CODE,
PDA.PO_DISTRIBUTION_ID,
AIDA.INVOICE_ID,
AIA.INVOICE_TYPE_LOOKUP_CODE AS INVOICE_TYPE,
AIA.REMIT_TO_SUPPLIER_NAME AS SUPPLIER_NAME,
AIA.REMIT_TO_SUPPLIER_ID AS SUPPLIER_ID,
AIA.INVOICE_DATE, AIA.INVOICE_NUM,
AIA.INVOICE_AMOUNT,
AIA.INVOICE_CURRENCY_CODE AS INVOICE_CURRENCY,
AIA.GL_DATE,
AIA.PAYMENT_CURRENCY_CODE AS PAYMENT_CURRENCY,
AIA.PAYMENT_CROSS_RATE_DATE AS PAYMENT_DATE,
AIA.DESCRIPTION, AIA.EXCHANGE_DATE,
AIA.EXCHANGE_RATE,
AIA.BASE_AMOUNT AS FUNCTIONAL_CURRENCY_AMOUNT
FROM
po_headers_all pha,
po_distributions_all pda,
ap_invoice_distributions_all aida,
ap_invoices_all aia,
po_requisition_lines_all prla,
po_req_distributions_all prda,
hr.hr_all_organization_units haou,
pa.pa_projects_all ppa
LEFT JOIN
xxwrp.xxwrp_service_reg_all reg
ON
reg.po_header_id = pha.po_header_id
WHERE
1=1
AND pha.segment1 = '201251390'
AND pda.req_distribution_id = prda.distribution_id
AND prda.requisition_line_id = prla.requisition_line_id
AND pha.po_header_id = pda.po_header_id
AND pda.po_distribution_id = aida.po_distribution_id
AND aida.invoice_id = aia.invoice_id
AND pda.project_id = ppa.project_id
AND haou.organization_id=ppa.carrying_out_organization_id
AND pha.creation_date > aia.invoice_date
GROUP BY
PHA.PO_HEADER_ID, PRLA.ATTRIBUTE1, PHA.SEGMENT1, REG.DOCUMENT_TYPE, HAOU.NAME, PHA.CREATION_DATE, PHA.AUTHORIZATION_STATUS, PHA.APPROVED_DATE, PHA.COMMENTS, PHA.CLOSED_DATE, PHA.CLOSED_CODE, PDA.PO_DISTRIBUTION_ID, AIDA.INVOICE_ID, AIA.INVOICE_TYPE_LOOKUP_CODE, AIA.REMIT_TO_SUPPLIER_NAME, AIA.REMIT_TO_SUPPLIER_ID, AIA.INVOICE_DATE, AIA.INVOICE_NUM, AIA.INVOICE_AMOUNT, AIA.INVOICE_CURRENCY_CODE, AIA.GL_DATE, AIA.PAYMENT_CURRENCY_CODE, AIA.PAYMENT_CROSS_RATE_DATE, AIA.DESCRIPTION, AIA.EXCHANGE_DATE, AIA.EXCHANGE_RATE, AIA.BASE_AMOUNT
When I run it, it returns this error:
ORA-00904: "PHA"."PO_HEADER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 50 Column: 21
The issue is in mixing the old Oracle syntax with the ANSI join syntax; for example, say you have these tables:
create table t1(n1 number);
create table t2(n2 number);
create table t3(n3 number);
this is what you get:
SQL> select *
2 from t1,t2
3 left join t3 on t3.n3 = t1.n1
4 where t1.n1 = t2.n2;
left join t3 on t3.n3 = t1.n1
*
ERROR at line 3:
ORA-00904: "T1"."N1": invalid identifier
SQL> select *
2 from t1
3 inner join t2 on t1.n1 = t2.n2
4 left join t3 on t3.n3 = t1.n1;
no rows selected
The problem is your left join. There is probably a column missing; the one you are joining on.
Here is more details on the error you're receiving:
https://www.techonthenet.com/oracle/errors/ora00904.php

SQL Error: ORA-00907: missing right parenthesis in Oracle 10g

Please find the below query. When I run this query I am getting an ORA-00907: missing right parenthesis error. I am unable to understand the problem on this query. Please give me the solution for this error. I have read some related topics on this topic. but, I didn't get the right solution. I am using Oracle 10g version.
The query:
SELECT A.City||'.' AS AAddress,
M_InOut_Header_v.Org_Location_ID AS Org_Location_ID,
M_InOut_Header_v.ContactName,
M_InOut_Header_v.Title,
M_InOut_Header_v.EMail,
M_InOut_Header_v.Phone,
M_InOut_Header_v.BPGreeting,
M_InOut_Header_v.Name2,
M_InOut_Header_v.Name,
B.City||'.' AS BAddress,
M_InOut_Header_v.C_Location_ID AS C_Location_ID,
M_InOut_Header_v.BPContactGreeting,
--M_InOut_Header_v.BPGreeting,
--M_InOut_Header_v.Name,
--M_InOut_Header_v.Name2,
M_InOut_Header_v.Address1,
M_InOut_Header_v.Address2,
M_InOut_Header_v.Address3,
M_InOut_Header_v.Address4,
M_InOut_Header_v.City,
M_InOut_Header_v.POSTAL1,
M_InOut_Header_v.CountryName,
--M_InOut_Header_v.BPContactGreeting,
--M_InOut_Header_v.Name,
M_InOut_Header_v.OrderRemarks,
(SELECT NVL(C_Order.DocumentNo,'')||'
- '||NVL(TRIM(TO_CHAR(C_Order.DateOrdered,'DD/MM/YYYY')),'')
FROM C_Order
WHERE M_InOut_Header_v.C_Order_ID=C_Order.C_Order_ID
) AS CC_Order_ID,
M_InOut_Header_v.C_Order_ID AS C_Order_ID,
M_InOut_Header_v.OrderType,
M_InOut_Header_v.ReferenceNo,
M_InOut_Header_v.POReference,
(SELECT NVL(M_Warehouse.Name,'')
FROM M_Warehouse
WHERE M_InOut_Header_v.M_Warehouse_ID=M_Warehouse.M_Warehouse_ID
) AS DM_Warehouse_ID,
M_InOut_Header_v.M_Warehouse_ID AS M_Warehouse_ID,
(SELECT NVL(M_Shipper.Name,'')
FROM M_Shipper
WHERE M_InOut_Header_v.M_Shipper_ID=M_Shipper.M_Shipper_ID
) AS EM_Shipper_ID,
M_InOut_Header_v.M_Shipper_ID AS M_Shipper_ID,
M_InOut_Header_v.Vehicle_No_BizInt,
M_InOut_Header_v.TrackingNo,
M_InOut_Header_v.Permit_No_BizInt,
M_InOut_Header_v.LR_Number_BizInt,
M_InOut_Header_v.FREIGHTCOSTRULE2,
M_InOut_Header_v.FreightAmt,
M_InOut_Header_v.POREMARKS,
M_InOut_Header_v.NoPackages,
M_InOut_Header_v.Gross_Weight_BizInt,
M_InOut_Header_v.UOMSymbol,
M_InOut_Header_v.SalesRepPhone,
M_InOut_Header_v.SalesRepEmail,
M_InOut_Header_v.DocumentType,
InOut_Header_v.DocumentNo,
M_InOut_Header_v.MovementDate,
M_InOut_Header_v.Description,
(SELECT NVL(M_InOut.DocumentNo,'')||'
- '||NVL(TRIM(TO_CHAR(M_InOut.MovementDate,'DD/MM/YYYY')),'')
FROM M_InOut
WHERE M_InOut_Header_v.M_InOut_ID=M_InOut.M_InOut_ID
) AS FM_InOut_ID,
M_InOut_Header_v.M_InOut_ID AS M_InOut_ID,
M_InOut_Header_v.DocumentTypeNote,
M_InOut_Header_v.REMARKS1,
M_InOut_Header_v.REMARKS2,
M_InOut_Header_v.REMARKS3,
M_InOut_Header_v.REMARKS4
FROM M_InOut_Header_v
LEFT OUTER JOIN C_Location A
ON (M_InOut_Header_v.Org_Location_ID=A.C_Location_ID)
LEFT OUTER JOIN C_Location B
ON (M_InOut_Header_v.C_Location_ID=B.C_Location_ID)
WHERE (M_InOut_Header_v.M_InOut_ID=1002241)
AND M_InOut_Header_v.AD_Client_ID IN (1000008,0)
AND M_InOut_Header_v.AD_Org_ID IN (1000099,1000098,0,1000100,1000096,1000097)
AND (A.C_Location_ID IS NULL
OR A.C_Location_ID NOT IN (
SELECT PA.Record_ID FROM AD_Private_Access AS PA
WHERE PA.AD_Table_ID = 162 AND PA.AD_User_ID <> 1013144
AND PA.IsActive = 'Y'
))
AND ( B.C_Location_ID IS NULL
OR B.C_Location_ID NOT IN (
SELECT ADP.Record_ID FROM AD_Private_Access AS ADP
WHERE ADP.AD_Table_ID = 162 AND ADP.AD_User_ID <> 1013144
AND ADP.IsActive = 'Y'
))
ORDER BY M_InOut_Header_v.DocumentNo;
This gives me:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 80 Column: 26
But I didn't find any mistake in that line. I think it is syntactically correct.
The line number is slightly misleading, but it's pointing to the start of the problem. The issue is with these two subqueries:
SELECT PA.Record_ID FROM AD_Private_Access AS PA
WHERE PA.AD_Table_ID = 162 AND PA.AD_User_ID <> 1013144
AND PA.IsActive = 'Y'
and:
SELECT ADP.Record_ID FROM AD_Private_Access AS ADP
WHERE ADP.AD_Table_ID = 162 AND ADP.AD_User_ID <> 1013144
AND ADP.IsActive = 'Y'
You cannot use AS to mark an alias for a table name, only (optionally) for a column name or expression. There isn't actually a missing parenthesis. It's hard to know exactly what the parser is thinking, but in this case it looks like it's trying to interpret the AS PA as a column alias for that subquery, and that implies that the subquery should have ended by now, and so there should have been a close parenthesis already. (Another option might have been to try to treat AS as the table alias, but then it would have had to try to decide what PA meant; plus AS is a keyword so it wouldn't be valid as an alias name anyway).
Just remove the AS keyword from both of those and it'll work (or move on to another error).
SELECT PA.Record_ID FROM AD_Private_Access PA
...
and:
SELECT ADP.Record_ID FROM AD_Private_Access ADP
...

Oracle, invalid identifier error

I have this query that should:
create a listing that identifies different items in the inventory
table on the number of colors available for each option
But I'm getting an error saying:
Error at Command Line:167 Column:66 Error report: SQL Error:
ORA-00904: "INV"."ITEM_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
SELECT inv.inv_id,
item1.item_id,
(SELECT COUNT(*)
FROM (SELECT DISTINCT i.color FROM inventory i WHERE i.item_id = inv.item_id))
FROM inventory inv
INNER JOIN item item1
ON item1.item_id = inv.item_id
GROUP BY inv.inv_id, item1.item_id;
It sounds like you just want
SELECT inv.inv_id,
item1.item_id,
COUNT( distinct inv.color )
FROM inventory inv
JOIN item item1 ON (item1.item_id = inv.item_id)
GROUP BY inv.inv_id, item1.item_id
There doesn't seem to be any reason to hit the inventory table again in a subquery.

Resources