update (select * from a
where link_id = '1' and rownum = 1
order by input_date desc)
set status = '1'
可以帮忙看一下这个语句有什么问题吗
update (select * from a
where link_id = '1' and rownum = 1
order by input_date desc)
set status = '1'
可以帮忙看一下这个语句有什么问题吗
"表是通过查询语句得出的一个新表" 由视图实现。
创建视图时去除限制 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行已更新。
提交完成。