oracle数据库中对用同一个表内的一个指定字段长度的数据更新另一个指定字段长度的数据要怎么写,下面是例子表a
code name
100 a
101 b
100001 c
100101001 d在oracle里能不能通过代码实现让所有code字段的长度为3位的name字段都变成code字段长度为6位的name的值?
最终得到这样的结果表a
code name
100 c
101 c
100001 c
100101001 d
code name
100 a
101 b
100001 c
100101001 d在oracle里能不能通过代码实现让所有code字段的长度为3位的name字段都变成code字段长度为6位的name的值?
最终得到这样的结果表a
code name
100 c
101 c
100001 c
100101001 d
改成这这样
code name
100 a
101 b
100001 c
101001 d
让100的name变成100001的
101的name变成101001的
csdn变冷清了?!!!!!
还有,实在没看懂你想要什么。你说要改name但是你的例子里好像改的是code吧
set name = (select code from table_a sa where sa.code like '%' || ma.code || and length(sa.code) = 6 and rownum = 1)
where length(code) = 3
没有测试:
UPDATE TABLE M
SET NAME = (SELECT DISTINCT NAME
FROM TABLE N
WHERE M.CODE = SUBSTR(N.CODE, 1, 3)
AND M.CODE != N.CODE)
WHERE EXISTS (SELECT 1
FROM TABLE N
WHERE M.CODE = SUBSTR(N.CODE, 1, 3)
AND M.CODE != N.CODE)
create or replace procedure p_table as
cursor c is select id,name from table_a where length(id)=6;
c1 %rowtype;
begin
open c;
fetch c into c1;
loop
update table_a set name=c1.name where id=substr(c1.id,1,3);
commit;
exit when c %notfound;
end loop;
end;
exec p_table;