这些错误是在数据不规范的环境下出现的 update student a set 班级简称= decode((select 班级简称 from class where 班级名称=a.班级名称 and rownum<2), null,a.班级简称, (select 班级简称 from class where 班级名称=a.班级名称 and rownum<2));
declare cursor cur is select * from class begin for class_info in cur loop update student set 班级简称=class_info.班级简称 where 班级名称=class_info.班级名称; end loop; end;
update student a set 班级简称 = (select 班级简称 from class b where a.班级名称 = b.班级名称) where exists (select 班级简称 from class b where a.班级名称 = b.班级名称);拿出一个答案讨论一下,看看可不可行。
update student a set 班级简称 = (select 班级简称 from class b where a.班级名称 = b.班级名称 and rownum<2) --为防止重复名称,使用rownum<2 where exists (select 班级简称 from class b where a.班级名称 = b.班级名称); 这样就可以了,楼主觉得这种脚本很高深?如何高深?请说说
licsth() ( ) 信誉:99 Blog 2006-11-24 10:48:21 得分: 0
一条sql不行吧!等高人。 =========== …… update student a set 班级简称=(select 班级简称 from class where 班级名称=a.班级名称)============ 那个表class里不会有重复的“班级名称”记录吧?有的话改为: update student a set 班级简称=( select 班级简称 from class where 班级名称=a.班级名称 where rownum=1 )
我这没环境 你试试
你写的sql能正常执行?我试过了,出错:ORA-01407:(...."班级简称") 不能用NULL更新。
注意,如果‘表class中的班级简称’有重复一定要用‘distinct’否则多条。
update student a set 班级简称=
decode((select 班级简称 from class where 班级名称=a.班级名称 and rownum<2),
null,a.班级简称,
(select 班级简称 from class where 班级名称=a.班级名称 and rownum<2));
cursor cur
is
select * from class
begin
for class_info in cur
loop
update student set 班级简称=class_info.班级简称
where 班级名称=class_info.班级名称;
end loop;
end;
set 班级简称 = (select 班级简称
from class b
where a.班级名称 = b.班级名称)
where exists (select 班级简称
from class b
where a.班级名称 = b.班级名称);拿出一个答案讨论一下,看看可不可行。
set 班级简称 = (select 班级简称
from class b
where a.班级名称 = b.班级名称 and rownum<2) --为防止重复名称,使用rownum<2
where exists (select 班级简称
from class b
where a.班级名称 = b.班级名称);
这样就可以了,楼主觉得这种脚本很高深?如何高深?请说说
一条sql不行吧!等高人。
===========
……
update student a
set 班级简称=(select 班级简称 from class where 班级名称=a.班级名称)============
那个表class里不会有重复的“班级名称”记录吧?有的话改为:
update student a
set 班级简称=(
select 班级简称 from class where 班级名称=a.班级名称
where rownum=1
)
至于重名一说可以不用去考虑,没有什么意义,只是想拿出来讨论一下。
谢谢大家的参与~~