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
Related
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
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();
I have a SQL server table in which there are 2 columns that I want to update either of their values according to a flag sent to the stored procedure along with the new value, something like:
UPDATE
table_Name
SET
CASE
WHEN #flag = '1' THEN column_A += #new_value
WHEN #flag = '0' THEN column_B += #new_value
END AS Total
WHERE
ID = #ID
What is the correct SQL server code to do so??
I thought M.Ali's comment was correct, so I've constructed this based on his suggestion.
I'm also assuming the status field is 'approved' or 'declined' as you say based on if it's populated or not. If there are any other conditions on the status field, offcourse you must add these to you where statements
BEGIN TRANSACTION
Update Payment
set post_date = new_postdate_value
account_num = new_account_num_value
pay_am = new_pay_am_value
pay_type = new_pay_type_value
authoriz = new_authoriz_value
where status is not null
UPDATE Payment
SET account_num = new_account_num_value
WHERE status is null
COMMIT TRANSACTION
Can anyone help me fix this script?
I get this error: Msg 4104, Level 16, State 1, Line 1 The multi-part
identifier "#Temp3.EmpID" could not be bound.
the error is coming from: -- Update into Keyshop.EmployeeTable
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
The script is pretty self explanatory but let me know if you need me to provide more info.
Thank you
-- Update into Keyshop.EmployeeTable
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
This isn't a valid update statement since it references a table not in the UPDATE Clause or the Optional FROM clause
UPDATE EmployeeTable
SET Status = 'False'
WHERE #Temp3.EmpID = EmployeeTable.EmpID
There are several ways to do it using IN is probably the easiest to understand
UPDATE EmployeeTable
SET Status = 'False'
WHERE EmployeeTable.EmpID IN (SELECT EmpID FROM #Temp3)
Switch these guys around. Instead of this:
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
Have this
UPDATE EmployeeTable SET Status = 'False' WHERE EmployeeTable.EmpID in (SELECT EmpID FROM #Temp3)
Edit: I missed that #Temp3.EmpID is a table, not a variable.
I want to update the table values by using hard coded values.here is my code :
BEGIN
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
END
I want to do it in the single update statement. Can anyone tell me the solution?
UPDATE emp_table SET expiry_dt = TO_DATE
('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id IN ('78629160','111020102','88888888');
Should do you. Edited the IN clause with various employee id's as yours were identical.
UPDATE emp_table
SET expiry_dt =
CASE
WHEN emp_id = '78629160' THEN TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHEN emp_id = '78629161' THEN TO_DATE('21.10.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
END
WHERE emp_id IN ('78629160', '78629161')
I assume the fact that you have the same ID twice was just a copy and paste error, just like the fact that both dates are identical.
Btw: what data type is emp_id? If that is a numeric type, get rid of the single quotes for the literals (numeric literals should not be quoted). They will prevent the usage of an index on that column!
Begin UPDATE emp_table SET expiry_dt = TO_DATE ('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),-- why you are using ',' here??
WHERE emp_id = '78629160';
Begin
UPDATE emp_table SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHERE emp_id = '78629160';
end;
It is running in my machine whithout any error and problem.. adn giving expected result also..
try once again without using that ','.