I am trying to create a standard UPDATE query for a table. However, if certain criteria are met, some columns should be included / excluded from the UPDATE statement.
For instance:
UPDATE TBL_PROJECT SET
REVISION_COUNT = V_REVISION_COUNT
,PRIMARY_BRANCH = IN_PRIMARY_BRANCH
,PROJECT_STATUS = IN_PROJECT_STATUS
...
WHERE PROJECT_ID = IO_PROJECT_ID
AND REVISION_COUNT = IO_REVISION_COUNT
RETURNING REVISION_COUNT INTO IO_REVISION_COUNT';
However, the table has two columns for presentation and approval. Therefore, if the status is set for submission or approval, I want these columns to be updated. eg.
IF IN_PROJECT_STATUS = 'SUB'
UPDATE TBL_PROJECT SET
SUBMITTED_DATE = SYSDATE
ELSIF IN_PROJECT_STATUS = 'APP'
UPDATE TBL_PROJECT SET
APPROVED_DATE = SYSDATE
END;
I also need to return REVISION_COUNT and the number of rows affected (rowcount) to check if the update was successful or not.
What is the best way to write this query? I assume that a dynamic query is better than with an if-elsif-else statement, with the entire query being almost duplicated in each block.