vivia0717 2022-11-11 16:31 采纳率: 50%
浏览 37
已结题

oracle更新语句问题

update (select * from a
where link_id = '1' and rownum = 1
order by input_date desc)
set status = '1'

可以帮忙看一下这个语句有什么问题吗

  • 写回答

4条回答 默认 最新

  • curating 2022-11-12 09:57
    关注

    "表是通过查询语句得出的一个新表" 由视图实现。
    创建视图时去除限制 rownum = 1 order by input_date desc 可以update 。

    create view test_view2 as select * from test_a
    where link_id = '1' 
    ;
    update test_view2
    set status='1';
    

    但是可以更新的视图有限制,不能包含:
    ● 集合操作符(set operator)
    ● DISTINCT 操作符
    ● 聚合函数(aggregate function)或分析型函数(analytic function)
    ● GROUP BY,ORDER BY,CONNECT BY,或 START WITH 字句
    ● 在 SELECT 之后的列表中使用collection expression
    ● 在 SELECT 之后的列表中使用子查询(subquery)
    否则报如下错误:
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 - "data manipulation operation not legal on this view"
    完整测试:

    create table test_a (link_id number,status char(2),input_date date);
    INSERT INTO test_a VALUES ('1','0',sysdate);
    INSERT INTO test_a VALUES ('2','1',sysdate);
    INSERT INTO test_a VALUES ('3','0',sysdate);
    INSERT INTO test_a VALUES ('4','1',sysdate);
    INSERT INTO test_a VALUES ('5','0',sysdate);
    commit;
    select * from test_a;
    
    update(select * from test_a
    where link_id = '1' and rownum = 1
    order by input_date desc)
    set status='1';
    --更新视图的前提:
    --没有使用连接函数,集合运算和组函数.创建视图的select语句中没有集合函数且没有GROUP BY,ONNECT BY ,START WITH子句及DISTINCT关键字.
    --创建视图的SELECT语句中不包含从基表列通过计算所得的列.创建视图没有包含只读属性.
    
    create view test_view as select * from test_a
    where link_id = '1' and rownum = 1
    order by input_date desc;
    
    update test_view
    set status='1';
    
    
    create view test_view1 as select * from test_a
    where link_id = '1' and rownum = 1
    ;
    update test_view1
    set status='1';
    
    create view test_view2 as select * from test_a
    where link_id = '1' 
    ;
    update test_view2
    set status='1';
    commit;
    select * from test_a;
    
    

    输出日志:

    
    Table TEST_A 已创建。
    
    
    1行已插入。
    
    
    1行已插入。
    
    
    1行已插入。
    
    
    1行已插入。
    
    
    1行已插入。
    
    提交完成。
    
    在行: 15 上开始执行命令时出错 -
    update(select * from test_a
    错误报告 -
    未知的命令
    
    
    在行: 16 上开始执行命令时出错 -
    where link_id = '1' and rownum = 1
    错误报告 -
    未知的命令
    
    
    在行: 17 上开始执行命令时出错 -
    order by input_date desc)
    错误报告 -
    未知的命令
    
    SP2-0158: 未知的 SET 选项开头 "status='1'..."
    
    View TEST_VIEW 已创建。
    
    
    在行: 25 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 25 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    
    在行: 30 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 30 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    
    在行: 30 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 30 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    提交完成。
    
    在行: 30 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 30 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    
    在行: 30 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 30 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    
    在行: 30 上开始执行命令时出错 -
    update test_view
    set status='1'
    错误位于命令行: 30 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    提交完成。
    
    View TEST_VIEW1 已创建。
    
    
    在行: 37 上开始执行命令时出错 -
    update test_view1
    set status='1'
    错误位于命令行: 37 列: 8
    错误报告 -
    SQL 错误: ORA-01732: 此视图的数据操纵操作非法
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:    
    *Action:
    
    View TEST_VIEW2 已创建。
    
    
    1行已更新。
    
    提交完成。
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 已采纳回答 8月13日
  • 创建了问题 11月11日

悬赏问题

  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout