简体   繁体   English

SET .. WHERE .. 批量在 SSMS v18.4 中抛出错误

[英]SET .. WHERE .. within batch throwing an error in SSMS v18.4

I am revisiting MS SQL after a few years, so apologies for a basic question!几年后我正在重新访问 MS SQL,因此对一个基本问题表示歉意!

I am performing some address data cleansing, to expand abbreviations like DR into 'DRIVE' RD into 'ROAD' etc to help with identifying duplicate addresses.我正在执行一些地址数据清理,将 DR 等缩写扩展为“DRIVE” RD 到“ROAD”等,以帮助识别重复地址。

I am exploring ways to optimise my query by taking my existing block of 10 sequential SET .. WHERE .. statements into a batch or some other method, as I am working on approximately 500k records.我正在探索通过将我现有的 10 个连续 SET .. WHERE .. 语句块转换为批处理或其他方法来优化我的查询的方法,因为我正在处理大约 500k 条记录。

When I converted my block of existing SET .. WHERE .. statements into a simple batch, this is throwing an "incorrect syntax near the keyword where" error.当我将现有的 SET .. WHERE .. 语句块转换为一个简单的批处理时,这会引发“关键字 where 附近的语法不正确”错误。 Does anyone have any ideas or suggestions to resolve this?有没有人有任何想法或建议来解决这个问题? Here is what the code looks like (with a cut down list of SET .. WHERE .. statements):下面是代码的样子(带有 SET .. WHERE .. 语句的精简列表):

declare @batchSize int = 10000;
declare @rows int = -1;

while @rows <> 0
begin
 raiserror('Beginning loop. Last ROWCOUNT was %d',0,1, @rows) with nowait;
 update top (@batchSize) DBO.POSTAL_ADDRESS_MATCH_1
 SET ADDRESS1=REPLACE(ADDRESS1,' DR',' DRIVE') WHERE RIGHT(ADDRESS1,3) LIKE ' DR'
 -- 9 other SET WHERE statements go here
 where ID between @startID and @startID + @batchSize;

 set @rows = @@ROWCOUNT;
 set @startID += @batchSize;
end;

Many thanks in advance for your help.非常感谢您的帮助。

That's not the correct syntax for the SET clause of an UPDATE statement.这不是UPDATE语句的SET子句的正确语法。 You're looking for CASE .您正在寻找CASE

 UPDATE TOP (@batchSize) DBO.POSTAL_ADDRESS_MATCH_1
 SET ADDRESS1 = 
   CASE 
     WHEN RIGHT(ADDRESS1,3) LIKE ' DR' THEN REPLACE(ADDRESS1,' DR',' DRIVE') 
     WHEN RIGHT(ADDRESS1,3) LIKE ' ST' THEN REPLACE(ADDRESS1,' ST',' STREET') 
     ...etc...
     ELSE ADDRESS1 --<---Leave it alone if there's not a matching pattern.
     END
 where ID between @startID and @startID + @batchSize;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM