我目前正在使用一个 sql 游标来查找一个表以更新另一个表。我有一张包含很多短语的表格。如果这些短语中的任何一个落入更新表中的任何列,我想更新另一个表以设置 1。我正在使用光标和字符来查找短语。光标需要很长时间,我只是想知道是否可以使用其他任何东西代替光标。谢谢。我正在使用 sql server,这是代码
declare @word varchar(max)
declare @aCursor cursor for
SELECT col from table
open acursor
fetch next from acursor into @word
while @@fetch_status=0
begin
SET @word = '' + @word + ''
UPDATE updatetable
SET updatecol = 'y'
FROM updatetable u, tableb b
WHERE u.id = b.id AND (CHARINDEX(@word, u.name) > 0 OR CHARINDEX(@word, u.city) >
fetch next from acursor into @word
end
close acursor
deallocate acursor
最佳答案
看看:http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx那应该让你继续前进。然而,我已经完全了解这个问题,对于单独的行操作,游标是可行的方法,性能与其他方法大致相同,可读性比其他方法好 10 倍,使代码维护更容易。
但是,我没有足够的细节来理解为什么你不能用更新语句解决这个问题。
关于sql - sql游标的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943477/