SQL update values based on join - sql

I'm trying to update values of one table based on criteria of second table.
But something is wrong. Can you advise what I'm a doing wrong?
UPDATE food_serve
SET food_serve_cost = food_serve_cost*1.15
FROM food_serve JOIN fooditem
ON fooditem.food_item_no = food_serve.food_item_no
WHERE food_type = 'M' ;
Using oracle sql. Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:

Hello I think you miss something in food_serve_cost You didnt use any alias
UPDATE ( SELECT food_serve.food_serve_cost ,
fooditem.food_serve_cost
FROM food_serve
INNER JOIN fooditem ON fooditem.food_item_no =
food_serve.food_item_no
WHERE fooditem.food_type = 'M')
SET food_serve.food_serve_cost=fooditem.food_serve_cost*1.15
Or You can use merge
MERGE into food_serve
USING fooditem
ON (fooditem.food_item_no = food_serve.food_item_no)
when matched then update SET
food_serve.food_serve_cost=fooditem.food_serve_cost*1.15
WHERE fooditem.food_type = 'M'

Seems you have ambiguous columns try add the table name for example:
UPDATE food_serve
SET food_serve.food_serve_cost = fooditem.food_serve_cost*1.15
FROM food_serve JOIN fooditem
ON fooditem.food_item_no = food_serve.food_item_no
WHERE fooditem.food_type = 'M' ;

You can make use of Merge Operator better in this situation
select * into #Target from fooditem WHERE food_type ='M'
MERGE food_serve AS T
USING #Target AS S
ON S.food_item_no = T.food_item_no
WHEN MATCHED THEN
UPDATE SET T.food_serve_cost = T.food_serve_cost*1.15

Related

Why does this UPDATE statement fail in H2?

This UPDATE statement works when run against Postgres, but fails when run against H2. What's the equivalent H2 statement?
UPDATE award_details
SET award_id = a.id
FROM awards a
WHERE a.award_details_id = award_details.id;
The H2 error message is:
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "​​[*]
UPDATE AWARD_DETAILS
SET AWARD_ID = A.ID
FROM AWARDS A
WHERE A.AWARD_DETAILS_ID = AWARD_DETAILS.ID"; SQL statement:
Your current update syntax looks to be Postgres style. Instead use correlated subquery update syntax:
UPDATE award_details aw
SET award_id = (SELECT a.id FROM awards a WHERE a.award_details_id = aw.id);

Not able to update columns of a table by bind parameters using sql developer

The following select statement returns one row:
SELECT *
FROM CM_APOLLO_DET
WHERE
CM_APOLLO_DET.DETAIL_ID = TRIM(:detailId) AND
CM_APOLLO_DET.HEADER_ID IN (SELECT HEADER_ID FROM CM_APOLLO_HDR
WHERE TRIM(FILE_NAME) = TRIM(:fileName));
Values of bind parameters are as follows:
detailId: 775686609762
filename:sample3.txt
but when I run following update statement, it updates zero rows.
UPDATE CM_APOLLO_DET
SET CM_APOLLO_DET.DIVISION = :div
WHERE
CM_APOLLO_DET.DETAIL_ID = TRIM(:detailId) AND
CM_APOLLO_DET.HEADER_ID IN (SELECT HEADER_ID FROM CM_APOLLO_HDR
WHERE TRIM(FILE_NAME) = TRIM(:fileName));
div=2030
detailId: 775686609762
filename:sample3.txt
This update where condition is same as above select statement.
You can try below using exists
update CM_APOLLO_DET set CM_APOLLO_DET.DIVISION=:div
where CM_APOLLO_DET.DETAIL_ID=trim(:detailId) and
exists (SELECT 1 from
CM_APOLLO_HDR where CM_APOLLO_DET.HEADER_ID=CM_APOLLO_HDR.HEADER_ID
and trim(FILE_NAME)=trim(:fileName)
);

What is wrong with my UPDATE statement?

I just want to update the status of a booking using bookingid.
UPDATE flightbooking
SET status 'C' AS cancelledbooking
FROM flightbooking
WHERE bookingid = 10001;
I get the following error:
ERROR: syntax error at or near "'C'"
LINE 2: SET status 'C' AS cancelledbooking
Any help?
I would suggest:
UPDATE flightbooking
SET status = 'C'
WHERE bookingid = 10001;
This should work in any database, assuming you have the right table and columns.
FROM is not part of the FROM clause necessarily. In addition, how it gets interpreted varies among databases. Once you fix the SET clause, Postgres would update all rows (I think); SQL Server would update the matching row; Oracle and MySQL would generate an error.
You cannot use an alias within an update statement.
UPDATE flightbooking
SET status = 'C'
FROM flightbooking
WHERE bookingid = 10001;
AS is used for aliasing in SELECT context, not UPDATE.
This will work:
UPDATE flightbooking
SET status = 'C'
FROM flightbooking
WHERE bookingid = 10001;
Simply:
UPDATE flightbooking SET status = 'C' WHERE bookingid = 10001

Where Clause is not working in Update but working in Select in Oracle DB

I am trying to execute the below update query
update custom_field cfe set cfe.field_value =:valueId where cp_entity_id = :cId
0 rows updated.
This is not updating any row but same where clause is working fine with select query and returns 1 row
select * from custom_field where cp_entity_id = :cId
Also, if i hardcode the value of cId parameter then update works fine but I am executing it from java program so it's not possible for me to hardcode the value
Also cp_entity_id column is a foreign key.
Try this, I faced similar issue.
Use this
select primary_key from custom_field where cp_entity_id = :cId query to find out primary key and Then use that primary key in your where clause of update query.
One of the ways to set parameter is explained here.
PreparedStatement ps = conn.prepareStatement(
"UPDATE Messages SET description = ?, author = ? WHERE id = ? AND seq_num = ?");
// set the preparedstatement parameters
ps.setString(1,description);
ps.setString(2,author);
ps.setInt(3,id);
ps.setInt(4,seqNum);
// call executeUpdate to execute our sql update statement
ps.executeUpdate();
ps.close();

Oracle SQL update from one Table to another table throws syntax error

Oracle SQL update from one Table to another table throws syntax error for following simple update query.
UPDATE Sales_Import SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID;
Error:
Error starting at line 1 in command:
Error at Command Line:2 Column:37
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
I believe the same query will work in other databases like postgres etc.
Could any one tell the correct query ?
and whatever i tried is this ANSI standard query ?
and whatever i tried is this ANSI standard query ?
No. Oracle Oracle doesn't support join in update statement.
In Oracle you could do it in two ways -
**Merge statement **
with only update clause in merge statement
MERGE INTO sales_import s
USING (SELECT *
FROM retrieveaccountnumber) u
ON (u.leadid = s.leadid)
WHEN matched THEN
UPDATE SET u.accountnumber = s.accountnumber;
Correlated query
UPDATE sales_import t1
SET accountnumber = (SELECT t2.accountnumber
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid )
WHERE EXISTS (
SELECT 1
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid );
I will write your sql like this:
UPDATE Sales_Import SI
SET AccountNumber = (Select RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID);
You can do this by joining those tables:
UPDATE SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
JOIN Sales_Import SI ON RAN.LeadID = SI.LeadID;