Oracle 中的动态更新查询
我正在尝试为表创建标准 UPDATE 查询。但是,如果满足某些条件,则应在 UPDATE 语句中包含/排除某些列。
例如:
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';
但是,该表有两列,分别是“提交者”和“批准者”。因此,如果状态设置为已提交或已批准,我希望更新这些列。例如。
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;
我还需要返回 REVISION_COUNT 和受影响的行数 (rowcount) 以检查更新是否成功。
编写此查询的最佳方式是什么?我假设动态查询比使用 if-elsif-else 语句更好,并且整个查询在每个块中几乎重复。
I'm 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 example:
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 submitted by and approved by. So if the status is set to submitted or approved, I want those 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 the REVISION_COUNT and number of rows affected (rowcount) to check if the update was successful or not.
What is the best way to write this query? I'm assuming a Dynamic query is better than having an if-elsif-else statement with the whole query nearly duplicated in each block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写一个
UPDATE
,并使用DECODE
(或CASE
)仅在in_project_status
匹配时更新日期:这将避免重复的
UPDATE
。You could write a single
UPDATE
, and useDECODE
(orCASE
) to only update the dates whenin_project_status
matches:This would avoid the duplicate
UPDATE
.案例示例:
Case example: