更新varshar

时间:2017-03-13 10:49:11

标签: sql oracle sql-update

我想进行此次更新

update c_order set documentno = 'OV1036477' where documentno = 'OV1036503';
update c_order set documentno = 'OV1036476' where documentno = 'OV1036502';

....等

所有这些文件没有(此文件没有订购[503 - 483]

OV1036503

OV1036502

OV1036501

OV1036500

OV1036499

OV1036498

OV1036497

OV1036496

OV1036495

OV1036494

OV1036493

OV1036492

OV1036491

OV1036490

OV1036489

OV1036488

OV1036487

OV1036486

OV1036485

OV1036484

OV1036483

问题:如何通过一次更新来完成此操作?

3 个答案:

答案 0 :(得分:1)

试试这个

update c_order set documentno = 'OV'||( to_number(substr(documentno,3))-26)
where documentno in ( 'OV1036503','OV1036502');

答案 1 :(得分:1)

使用以下查询更新所有行:

UPDATE c_order SET documentno = 'OV' + CAST( RIGHT(documentno,7) - 26 AS 
VARCHAR)  

If you need WHERE Clause means :

UPDATE c_order SET documentno = 'OV' + CAST( RIGHT(documentno,7) - 26 AS  
VARCHAR) 
--WHERE Condition 

答案 2 :(得分:1)

你能详细说明一下,以便我们能做出更合适的答案吗?

替换中有一些对应关系?就像你想要将每一端更改为-x值或者该过程背后有一个逻辑?

无论如何,我想这样的事情可以解决问题:

set serveroutput on size unlimited

declare
 type numListType is table of number;
 numList numListType;
 numList2 numListType;
begin
numList := numListType(
 503, 502, 501, 500, 499, 498, 497, 496, 495, 494, 493, 492, 491, 490, 489, 488, 487, 486, 485, 484, 483
);
numList2 := numListType(
 477, 476, 475, 474, 473, 472, 471, 470, 469, 468, 467, 466, 465, 464, 463, 462, 461, 460, 459, 458, 457
);
for i in numList.FIRST..numList.LAST loop
     EXECUTE IMMEDIATE 'update c_order set documentno = ''OV1036'|| 
                  numList2(i) || ''' where documentno = ''OV1036'|| numList(i)||'''';
end loop;
end;
/