0

I am fairly new to posting questions i greatly appreciate your support. I wanted to write a proc to update my table testAd.

A replace function could do it but I have almost a million records to update and I am trying to use stored procedure.

Could you have a look at what I have started? Thank you I appreciate it. I should come up with something by end of day today..a kind of tough situation. I tried the one below it doesn't seem to be working.

CREATE PROC [dbo].[testAd]
AS
DECLARE @ci  VARCHAR (255)
UPDATE Stage.TestAdd
SET @ci=AddressOne
SET @ci= CASE @ci
 WHEN 'Des Moines'  THEN 'IA' 
 WHEN 'Seattle'    THEN 'WA'
 WHEN 'Pheonix'    THEN 'Az'   
 WHEN 'Phx'   THEN 'Az'
 .
 . 
 ELSE @ci

END

4

1 回答 1

0

如果我理解正确,您希望将存储在一个字段中的值从 [city] 更新为匹配 [state]

您可以使用具有有效对的表变量而不是使用长 case 语句,然后在更新中加入该表

像这里的东西:

declare @cityInState as table(city varchar(50), state char(2))
insert into @cityInState 
values
    ('Des Moines','IA') 
    ,('Seattle', 'WA')
    ,('Pheonix','Az')   
    ,('Phx','Az')
    -- etc ...

update yt
set 
    fieldToUpdate = cis.state
from 
    yourTable yt
    inner join @cityInState cit on yt.fieldToUpdate = cit.city 

警告:这将用州替换城市值,所以只有在你想要的时候才运行它。

将其添加为额外字段(理想情况下是对查找表的引用)会更好,因为更新后您将丢失城市信息。

于 2012-09-21T15:33:46.620 回答