Why I get error SQL command not properly ended while I execute UPDATE query - oracle

I am trying to migrate such a simple MySQL query to Oracle SQL using PLSQL
UPDATE submaterial SET `Name` = '{$name}'".($image !== null ? ", `Picture` = '{$image}'" : "")." WHERE SubmaterialID = {$id};
When I migrate to PLSQL I get something like this
UPDATE submaterial SET Name = p_name
CASE WHEN p_image != NULL THEN
Picture = p_image
WHERE SubmaterialID = p_Id;
When I want to execute this I get error
Error(408,6): PL/SQL: ORA-00933: SQL command not properly ended
I try also something like this but also get same error message
UPDATE submaterial SET Name = p_name
CASE WHEN p_image IS NOT NULL THEN
Picture = p_image
WHERE SubmaterialID = p_Id;
Does anyone know where did I made mistake ? How to write this king of query to Oracle SQL ?

That would be
UPDATE submaterial
SET Name = p_name,
picture = CASE WHEN p_image IS NOT NULL THEN p_image END
WHERE SubmaterialID = p_Id;
i.e. you have to take picture = out of case expression.

Related

PL/SQL: ORA-00936: missing expression while using trigger

I am trying to update a table in one database from another using a trigger. I am getting the missing expression error which i am not able to figure out the reason why. The error is at the line :new.FO_CD != :old.FO_CD. I am also getting PL/SQL: SQL Statement ignored error at the line :new.END_DT != :old.END_DT OR. What might be causing these two errors?
CREATE OR REPLACE TRIGGER TRG_UPDATE_ITIN
AFTER DELETE OR UPDATE
ON FDL.PLA_ET
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF DELETING THEN
UPDATE
FMS.ITIN
SET
PLN_ET_UUID = NULL
WHERE
PLN_ET_UUID = :OLD.PLN_ET_UUID;
ELSIF UPDATING THEN
IF
:new.START_DT != :old.START_DT OR
:new.END_DT != :old.END_DT OR
:new.CREATED_DTTM != :old.CREATED_DTTM OR
:new.CREATED_USER_ID != :old.CREATED_USER_ID OR
:new.ACT_UUID != :old.ACT_UUID OR
:new.FO_CD != :old.FO_CD
THEN UPDATE FMS.ITIN
SET
START_DATE = :new.START_DT,
END_DATE = :new.END_DT,
CREATION_DATE = = :new.CREATED_DTTM,
CREATED_BY_USER = :new.CREATED_USER_ID,
ACT_UUID = :new.ACT_UUID,
FO_CTL_NBR = :new.FO_CD
WHERE
PLN_ET_UUID = :new.PLN_ET_UUID;
END IF;
END IF;
END;
This is wrong:
UPDATE fms.itin SET
start_date = :new.start_dt,
end_date = :new.end_dt,
creation_date = = :new.created_dttm, --> here
created_by_user = :new.created_user_id,
You've got two consecutive = = signs.
Maybe there are other errors, but without your tables, I can't run code you posted. Consider posting CREATE TABLE statement.

Oracle sql update only if id exist else return an error message

In mongoDB i remember there is a findOneAndUpdate query. Do we have or atleast can we create a query in oracle sql that only updates record if it exists otherwise will return a no data found message? I tried this query below however it updates all the record when a result is found:
UPDATE
user
SET
fname=:lname,
lname=:fname
WHERE
EXISTS (
SELECT * FROM user WHERE id=:id
)
Wouldn't it be just
update t_user set
fname = :lname,
lname = :fname
where id = :id;
It won't return an error if nothing's being updated, though, so - if it is actually a PL/SQL procedure, you'll have to check it yourself, e.g. (right behind the above UPDATE statement):
if sql%rowcount = 0 then
dbms_output.put_line('No rows have been updated');
end if;

when i run the procedure i get this error [Err] ORA-24344: success with compilation error missing keyword

CREATE OR REPLACE
PROCEDURE "UNASSIGN_CUSTOMER_FEATURES"
(CustomerID_Param IN NUMBER, FeatureID_Param IN NUMBER, WalletID_Param IN NUMBER)
AS
BEGIN
DELETE FROM CUSTOMER_EXTRA_FEATURES WHERE FEATURES_ID = FeatureID_Param
AND CUSTOMER_ID = CustomerID_Param;
MERGE INTO CUSTOMER_SERVICE_CONFIG c
USING
(SELECT BUSINESS_SERVICE_CONFIG.ID from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param ) ids
ON (c.SERVICE_CONFIG_ID = ids.ID and c.CUSTOMER_ID = CustomerID_Param )
WHEN MATCHED THEN
DELETE WHERE CUSTOMER_ID = CustomerID_Param AND SERVICE_CONFIG_ID = ids.ID;
END;
You are missing an UPDATE statement prior to doing a DELETE
something like
WHEN MATCHED THEN
UPDATE SET <some field> with <some value>
DELETE WHERE CUSTOMER_ID = CustomerID_Param AND SERVICE_CONFIG_ID =ids.ID;
END;
Refer the following url:
Oracle sql merge to insert and delete but not update
Hope that helps!
The syntax diagram for the merge statement shows that you the delete clause is an optional part of the update, not standalone:
You're getting the "ORA-00905: missing keyword" error because you don't have an update. You could put in a dummy update, but it looks like you really want to delete all rows that match, with no updates to other rows or inserts of new rows; which would be simpler as a plain delete, something like:
DELETE FROM CUSTOMER_SERVICE_CONFIG c
WHERE CUSTOMER_ID = CustomerID_Param
AND SERVICE_CONFIG_ID IN (
SELECT BUSINESS_SERVICE_CONFIG.ID from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param );
or
DELETE FROM CUSTOMER_SERVICE_CONFIG c
WHERE CUSTOMER_ID = CustomerID_Param
AND EXISTS (
SELECT null from BUSINESS_SERVICE_CONFIG join SERVICE_CONFIG_MAP
ON BUSINESS_SERVICE_CONFIG.Business_SERVICE_TYPE = SERVICE_CONFIG_MAP.Service_type_ID
and BUSINESS_SERVICE_CONFIG.ORGANIZATION_ID = WalletID_Param
and BUSINESS_SERVICE_CONFIG.BUSINESSSERVICECATEGORY = 0
and SERVICE_CONFIG_MAP.FEATURES_ID = FeatureID_Param
WHERE BUSINESS_SERVICE_CONFIG.ID = c.SERVICE_CONFIG_ID );

Getting errors when trying to insert data into a table in Oracle

I am using python 2.7 and the cx_Oracle module. When I try to run the following query:
UPDATE bs_cart
SET qty = qty + :moreBooks
WHERE userid = :userID
AND isbn = :bookNumber;
IF SQL%ROWCOUNT = 0 THEN
INSERT INTO bs_cart
(userid,isbn)
VALUES
(:userID,:bookNumber)
using Cursor.execute() from cx_Oracle I get the following error:
DatabaseError: ORA-00911: invalid character
When I put it in SQL plus it says:
SP2-0734: unknown command beginning "IF SQL%ROW..." - rest of line ignored.
I am trying to make the cart UPDATE if a user already has the selected book in the cart and INSERT if there are no current copies of the book they want in the cart.
The execute method looks like:
ds.execute(sql,moreBooks=howMany,userID=userLoggedIn,bookNumber=booksToBuy)
and each of the parameters are user generated using rawinput() and then checked against a regular expression.
You need to enclose your statements in begin/end Oracle blocks.
Something like:
BEGIN
UPDATE bs_cart
SET qty = qty + :moreBooks
WHERE userid = :userID
AND isbn = :bookNumber;
IF SQL%ROWCOUNT = 0 THEN
INSERT INTO bs_cart (userid,isbn)
VALUES (:userID,:bookNumber);
END;

Update table with if statement PL/SQL

I am trying to do something like this but am having trouble putting it into oracle coding.
BEGIN
IF ((SELECT complete_date FROM task_table WHERE task_id = 1) IS NULL)
THEN
UPDATE task_table SET complete_date = //somedate WHERE task_id = 1;
ELSE
UPDATE task_table SET complete_date = NULL;
END IF;
END;
But this does not work. I also tried
IF EXISTS(SELECT complete_date FROM task_table WHERE task_id = 1)
with no luck.
I don't think you'd need the procedural block if your actual logic is like the one above.
Assuming this is your task:
"if the value of complete_date for id
1 is NULL, update it with XXX. Otherwise set it to null".
You could just run ...
Update task_table
set complete_date = nvl2(complete_date,NULL, <**your date**>)
where task_id = 1;
This will only update those records where the complete_date is null with your new date.
What is the intention of this part?
IF ((SELECT complete_date FROM task_table WHERE task_id = 1) IS NULL)
Is it to find if there are rows where complete_date is null and task_id = 1?
Or is it to check if there are no rows where task_id = 1?

Resources