日期:2014-05-17  浏览次数:20798 次

菜鸟求PL-SQL语句
表如下所示
code1 code2 code3
01 null str1
0101 str1 1234
0102 str1 2345
02 null str2
0201 str2 456789
要求:例如第二行code2的值(str1)=第一行的code3时,把第二行的code2替换成第一行的code1
code1 code2 code3
01 null str1
0101 01 1234
0102 01 2345
02 null str2
0201 01 456789

------解决方案--------------------
create table test1(
 code1 varchar2(20),
 code2 varchar2(20),
 code3 varchar2(20))

insert into test1
select '01' code1,null code2,'str1' code3 from dual union all
select '0101','str1','1234' from dual union all
select '0102','str1','2345' from dual union all
select '02',null,'str2' from dual union all
select '0201','str2','456789' from dual
commit;
select * from test1;


 
 update test1 t1 set code2 = (select code1 from test1 t2 where t1.code2 = t2.code3)