sql - 在子查询中生成值的 Oracle SQL 更新语句
问题描述
我正在尝试编写一个更新语句来插入一个在子查询中计算的值,并且成功率有限。
到目前为止我尝试过的声明是:
update intuit.men_doc doc1
set doc1.doc_udf5 = (select
substr(doc.doc_dtyc, instr(doc.doc_dtyc, 'GAPP-', 2)+5 )||'_'||row_number() over(partition by
doc.doc_dtyc order by doc.doc_cret) docDeleteId
from
intuit.men_doc doc
where
doc.doc_dtyc != 'DM-GAPP-SFUL'
and doc.doc_dtyc like 'DM-GAPP%'
and doc.doc_cred >= '01/Oct/2017' and doc.doc_cred < '01/Oct/2018'
and doc1.doc_code = doc.doc_code
)
这给了我以下错误消息
ERROR: Error 1427 was encountered whilst running the SQL command. (-3)
Error -3 running SQL : ORA-01427: single-row subquery returns more than one row
我对 UPDATE 语句没有太多经验,所以任何关于如何重写它以便我可以一次更新几千条记录的建议将不胜感激。
编辑:添加示例数据
示例数据:
MEN_DOC
DOC_CODE DOC_DTYC DOC_UDF5 DOC_CRED
123456A CV 08/Nov/2017
456789B CV 11/Jan/2018
789123C CV 15/Feb/2018
123987B TRAN 01/Dec/2017
脚本运行后我希望数据如何显示
MEN_DOC
DOC_CODE DOC_DTYC DOC_UDF5 DOC_CRED
123456A CV CV_1 08/Nov/2017
456789B CV CV_2 11/Jan/2018
789123C CV CV_3 15/Feb/2018
123987B TRAN TRAN_1 01/Dec/2017
谢谢
解决方案
您正在使用row_number()
,这表明您希望子查询返回多行。上的不等式doc_code
支持这种解释。
只需将 更改row_number()
为count(*)
,这样您就有了一个聚合,它将始终返回一行并获得您想要的顺序计数:
update intuit.men_doc doc1
set doc1.doc_udf5 = (select substr(doc.doc_dtyc, instr(doc.doc_dtyc, 'GAPP-', 2)+5 ) ||'_'|| count(*) docDeleteId
from intuit.men_doc doc
where doc.doc_dtyc <> 'DM-GAPP-SFUL' and
doc.doc_dtyc like 'DM-GAPP%' and
doc.doc_cred >= date '2017-10-01' and
doc.doc_cred < date '2018-10-01' and
doc1.doc_code = doc.doc_code
);
推荐阅读
- angular - Angular 12:TypeError:teardown.unsubscribe 不是函数
- pimcore - pimcore 中的静态路由不适用于多个参数
- ansible - ansible:自定义要使用的密码管理器
- python-3.x - 数据类 + sqlalchemy(使用 mapper_registry.mapped)无法实例化对象
- json - 如何通过`jq`在一行中打印多个值?
- c - CS50 过滤器:边缘我的代码工作正常,但无法通过 cs50 检查
- r - R 无法连接到 SQLite
- react-native - 使用 expo 安装带有 NPM 的包失败
- browser - 如何在 Allure 框架中禁用缓存?
- c# - 单元测试中的模拟混淆