I have following Oracle update statement
UPDATE details
SET details.ISO_BNK_TX_CODE_PRTRY_CODE = details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
FROM AS_ACCT_STAT_DET details
JOIN AS_ACCT_STAT statements ON details.ACCT_STAT_ID = statements.ID
JOIN MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE fdef.CODE = 'BAI2'
/
I made the original script in SQL Server. But I thought this code would work for both. But I receive following error message:
Error at Command Line : 4 Column : 1 Error report - SQL Error:
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
I think something is missing between the last set and the FROM statement.
Anyone?
Try this query
UPDATED
UPDATE (
Select
details.ISO_BNK_TX_CODE_PRTRY_CODE ,details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER
FROM
AS_ACCT_STAT_DET details
INNER JOIN
AS_ACCT_STAT statements ON details.ACCT_STAT_ID = statements.ID
INNER JOIN
MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE
fdef.CODE = 'BAI2'
) d
SET
d.ISO_BNK_TX_CODE_PRTRY_CODE = d.TX_TYP_CODE,
d.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
Oracle doesn't allow a join in an update statement, without a subquery anyway. You may be able to use an updatable view here, but you don't need to as you aren't getting the new values from one of the joined tables.
So it looks like you can just do:
UPDATE details
SET details.ISO_BNK_TX_CODE_PRTRY_CODE = details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
WHERE EXISTS (
SELECT null
FROM AS_ACCT_STAT statements
JOIN MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE statements.ID = details.ACCT_STAT_ID
AND fdef.CODE = 'BAI2'
)
/
That will only update rows where there is matching data in the other two tables, from the exists() condition; that subquery is correlated with the table being updated via its where clause.
Related
I am trying to recreate a Sql Server stored procedure in my Node app, which uses the mssql npm package. Right now, when I try and run the following query, I get an incorrect SQL error:
UPDATE
dbo.C2980251
RIGHT JOIN CFRTR...dbo.UPR10301 ON dbo.C2980251.BACHNUMB = dbo.UPR10301.BACHNUMB
SET
dbo.C2980251.UPRBCHOR = dbo.UPR10301.uprbchor,
dbo.C2980251.BACHNUMB = dbo.UPR10301.bachnumb,
dbo.C2980251.TRU_TYPE_ID = "132"
WHERE
(((dbo.C2980251.BACHNUMB) Is Null))
The specific error is this:
Incorrect syntax near the keyword 'RIGHT'.
To clarify, CFRTR, refers to the db being targeted. All of the tables being used here are from the same db.
It's not clear to me what the error is here. How should this be written?
The correct syntax is:
UPDATE c
SET UPRBCHOR = u.uprbchor,
BACHNUMB = u.bachnumb,
TRU_TYPE_ID = 132
FROM dbo.C2980251 c LEFT JOIN
CFRTR.dbo.UPR10301 u
ON u.BACHNUMB = c.BACHNUMB
WHERE u.BACHNUMB Is Null
Notes:
SQL Server requires a separate FROM clause.
Table aliases make the query much easier to write and read.
A LEFT JOIN makes much more sense here than a RIGHT JOIN. The database cannot update records that don't exist.
u.uprbchor and u.bachnumb are going to be NULL because the WHERE clause only keeps rows with no matches.
Hi I try to adapt this blog post to my needs using Oracle to model a Data Vault architecture.
With the following code I try to build a link between two hubs referencing on the original source table "orders".
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders as src
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order AS dest
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
However I receive following error.
Error at Command Line : 192 Column : 17
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Any help or hint much appreciated!
There are two places where you have used the as keyword. For giving alias to the table you just need to write the name of the alias after table name. as should not be used there.
Try the following code: (changes are mentioned inline)
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders src -- removed AS from here
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order dest -- removed AS from here
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
Cheers!!
Remove the as in your query i.e. instead of
FROM orders as src
use
FROM orders src
Oracle doesn't support the as alias, unless it is/will be added in most current versions
So i am trying to delete some rows in a left joined table using the following code in sql:
DELETE gw_svd_prefix_assignment
FROM gw_svd_prefix_assignment svdp
left join assyst_view av
on upper(svdp.user_name) = upper(av.usr_sc)
where upper(av.usr_sc) IS NULL
commit;
but i am getting this error:
Error starting at line : 1 in command -
DELETE gw_svd_prefix_assignment
FROM gw_svd_prefix_assignment svdp
left join assyst_view av
on upper(svdp.user_name) = upper(av.usr_sc)
where upper(av.usr_sc) IS NULL
commit
Error at Command Line : 2 Column : 1
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Oracle does not support that syntax. UPDATE and DELETE only work on one table . . . the FROM clause cannot contain multiple tables.
Instead, put the logic in the WHERE clause:
delete from gw_svd_prefix_assignment svdp
where not exists (select 1
from assyst_view av
where upper(svdp.user_name) = upper(av.usr_sc)
);
UPDATE s
SET s.TECH_NAME = r1.TECH_NAME
FROM **ERRMSG** s INNER JOIN **RAWDATA** r1 on
s.id = r1.id;
I want to update tech_name in table ERRMSG from table RAWDATA.Joining condition is ID.Whats wrong in the above query.Im getting the following error
Error at Command Line : 62 Column : 1
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
This sounds like a job for the MERGE statement documented here:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm
This is more in the spirit of what you were trying to do, it avoids correlated subqueries, and it gives you more flexibility.
merge into errmsg s
using rawdata r1
on (s.id = r1.id)
when matched then update set s.tech_name = r1.tech_name
If needed, you can use the other capabilities of MERGE as well; for example, when NOT matched, the id in the errmsg table does not have a corresponding id in the rawdata table. Would you like to take an action in that case as well? You could, for example, set the tech_name to NULL or some default value or delete the row altogether - you can do all these things in one MERGE statement. See the documentation I provided.
What is wrong is that you are using syntax that Oracle does not support. Use a correlated subquery instead:
UPDATE errmsg s
SET s.TECH_NAME = (SELECT r1.TECH_NAME FROM rawdata r1 WHERE s.id = r1.id)
WHERE EXISTS (SELECT 1 FROM rawdata r1 WHERE s.id = r1.id)
I am attempting to update a field based upon data in a joined table. I've read that the Update command will not work with a table joins in the where clause. However, I cannot use the Exists command workaround as my condition is not the existence of a linked record, but rather a value in that linked record.
update stock S
set stm_auto_key=186086
From
STOCK Left Join
STOCK_RESERVATIONS On STOCK.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
STOCK_RESERVATIONS.IND_AUTO_KEY Is Null
The select statement works fine stand alone. However using it in an update command yields "SQL command not properly ended."
Thanks in advance...
I guess, you need something like this, because there might be no FROM clause in UPDATE statement:
update stock S
set stm_auto_key=186086
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
(SELECT STOCK_RESERVATIONS.IND_AUTO_KEY FROM STOCK_RESERVATIONS
WHERE S.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY) Is Null