Alternative to sql course

I am currently using sql cursor to find a table to update another table. I have a table that contains many phrases. I want to update another table to be set to 1 if any of these phrases hit any column in the update table. I am using cursor and char to find a phrase. The cursor takes a long time and I'm just wondering if I can use anything else instead of the cursor. Thank you. I am using sql server and here is the code

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

      

+3


source to share


1 answer


Take a look at: http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx which should help you. I have however come to a complete cricle on this issue, for individual string manipulations, cursors are the way to go, performance is about the same as other methods, and readability is 10x better than other methods to make code easier to maintain.



However, I don't have enough details, it seems that you understand why you cannot fix this problem with the update instructions.

+5


source







All Articles