我从Oracle数据库运行了一堆查询(简单的更新查询但使用Replace函数)。我知道他们应该在准备脚本之前更新行。
他们看起来像下面这样:
update Table_X set VALUE = REPLACE (VALUE,'X0011' ,'ENG=X0011' ) where ColumnID = '23073' and Desc = 'label';
update Table_X set VALUE = REPLACE (VALUE,'A0011' ,'ENG=X0011' ) where ColumnID = '23073' and Desc = 'label';
update Table_X set VALUE = REPLACE (VALUE,'X0022' ,'ENG=X0022' ) where ColumnID = '23074' and Desc = 'label';
update Table_X set VALUE = REPLACE (VALUE,'A0022' ,'ENG=X0022' ) where ColumnID = '23074' and Desc = 'label';
update Table_X set VALUE = REPLACE (VALUE,'X0033' ,'ENG=X0033' ) where ColumnID = '23075' and Desc = 'label';
update Table_X set VALUE = REPLACE (VALUE,'A0033' ,'ENG=X0033' ) where ColumnID = '23075' and Desc = 'label';
数据是这样的:Table_X:
ID ColumnID Value Desc
________________________________
1 23073 A0011 label
2 23074 A0022 label
3 23075 A0033 label
我运行了两组数据的脚本:
A00xx
替换为X00yy
- >这将返回0行更新。我希望这里有更新。A00yy
替换为A00xx
- >这运行正常。 (当我在下面的查询中使用正确的数字运行相应的脚本时,它们会显示每行更新1行,但我希望只有一行可以替换它)。是否有人看过这种行为或能提供任何线索?
提前致谢 Ť
答案 0 :(得分:0)
为您的数据提供样本,这是您当前的陈述会发生的事情:
--will result in 1 row updated with no change
update Table_X set VALUE = REPLACE (VALUE,'X0011' ,'ENG=X0011' ) where ColumnID = '23073' and Desc = 'label';
-- will result in 1 row updated with value field as ENG=X0011
update Table_X set VALUE = REPLACE (VALUE,'A0011' ,'ENG=X0011' ) where ColumnID = '23073' and Desc = 'label';
-- will result in 1 row updated with no change
update Table_X set VALUE = REPLACE (VALUE,'X0022' ,'ENG=X0022' ) where ColumnID = '23074' and Desc = 'label';
--will result in 1 row updated with value field as ENG=X0022
update Table_X set VALUE = REPLACE (VALUE,'A0022' ,'ENG=X0022' ) where ColumnID = '23074' and Desc = 'label';
-- will result in 1 row updated with no change
update Table_X set VALUE = REPLACE (VALUE,'X0033' ,'ENG=X0033' ) where ColumnID = '23075' and Desc = 'label';
-- will result in 1 row updated with no change value field as ENG=X0033
update Table_X set VALUE = REPLACE (VALUE,'A0033' ,'ENG=X0033' ) where ColumnID = '23075' and Desc = 'label';
REPLACE
命令将在您的字段中查找第二个参数,如果发现它将替换为第三个参数。
由于您正在对where子句进行过滤,因此所有给定的行都将更新,但并非所有行都已更改,因为replace命令在该字段上找不到给定的字符串。
此处:VALUE = REPLACE (VALUE,'X0011','ENG=X0011')
您要说的是:如果字段值包含此字符串X0011
,则将其替换为ENG=X0011
,因为对于该已过滤的行,它不会执行任何操作。
如果要使此命令不更新任何行:
update Table_X set VALUE = REPLACE (VALUE,'X0011' ,'ENG=X0011' ) where ColumnID = '23073' and Desc = 'label';
您必须在where
过滤器上添加该字符串,例如
update Table_X
set VALUE = REPLACE (VALUE,'X0011' ,'ENG=X0011' )
where ColumnID = '23073'
and Desc = 'label'
and value = 'X0011';
答案 1 :(得分:0)
为什么不能这样:
update table_x set value = case when value not like 'ENG=' then 'ENG=X'||substr(value, 2, length(value)) else value end;