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.
Related
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;
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.
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 );
Seems like this should be easy, but the syntax eludes me. I am trying to update an existing row -- the first one I find that is 'New'. Then I want to return a delimited string having the key and 2 other values:
create or replace procedure usp_direct_print_task_deque
(
taskNa in char
,environmentCd in char
) as
begin
declare rowkey number;
select '~' || rowversion || '~' || target_document || '~' || target_printer
as delimited_data
, rowversion
from AFD1.bt_direct_print_tasks
where environment_cd = environmentCd
and task_na = taskNa
and task_status = 'new'
and rownum = 1;
update bt_direct_print_tasks
set taskStatus = 'processing'
where rowversion = rowkey;
return delimited_data;
end usp_direct_print_task_deque;
You need to define OUT parameters variables, say x_delimited_data and x_rowkey to hold the values for delimited_data and rowkey, respectively. Then change your SELECT to SELECT... INTO x_delimited_data, x_rowkey. (I'm assuming rowkey is what you are selecting as rowversion). Then change return delimited_data; to just return;
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?