0

我有这个问题要问你,为什么这会给我与原始相同字段“Ohio”中的原始值的更新值......现在在 proc“Ohio OH”之后我只想要更新的值。Th 函数只返回我在 proc 中使用它之前测试了一个值。提前感谢您调查它。

CREATE PROC [dbo].[Changestringstate] 
AS 
    UPDATE stage.statetable 
    SET    originalstatename = Rtrim(originalstatename) --avoiding trailing space issues 

    UPDATE stage.statetable 
    SET    originalstatename = Replace(originalstatename, 
                               Substring (originalstatename, 
                               Len (originalstatename) - ( 
                               Charindex(' ', Reverse( 
                               originalstatename)) ) + 1, Len(originalstatename) 
                               ), 
           dbo.Changefunction(originalstatename)) 
4

1 回答 1

0

考虑这个例子

create table statetable
(
  originalstatename varchar(100)
);
insert statetable values ('ohio oh');
GO

CREATE PROC [dbo].[Changestringstate] 
AS 
    UPDATE statetable 
    SET    originalstatename = Rtrim(originalstatename) --avoiding trailing space issues 

    UPDATE statetable 
    SET    originalstatename = Replace(originalstatename, 
                               Substring (originalstatename, 
                               Len (originalstatename) - ( 
                               Charindex(' ', Reverse( 
                               originalstatename)) ) + 1, Len(originalstatename) 
                               ), 
           'ohia') 
GO
exec changestringstate;
select * from statetable;

--- result
ohioohia

你的过程在做什么:

整个 SUBSTRING 块做一件事,就是删除 LAST SPACE 之后的所有内容。这不包括尾随空格,因为它们在之前的 UPDATE 中被修剪掉了。它的作用示例:

state name    |  Substring result
Ohio OH          " OH"
Mi Ohio OH       " OH"
New York NY      " NY"

然后是REPLACE块,它查找substring result,并用来自 的任何内容替换它的每一次出现。假设它以“xyz”返回。dbo.Changefunction()

state name    |  Substring result | after replace
Ohio OH          " OH"            | Ohioxyz
Mi Ohio OH       " OH"            | Mixyzioxyz
New York NY      " NY"            | New Yorkxyz

希望这可以帮助您了解您的过程,以便您可以相应地修复它。

于 2012-10-06T04:11:15.880 回答