Re: Text replacing from another table

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




I agree with Plamen, it would be helpful if you told us what the result you
wanted was. I assume with the sample data you gave, the result you want is:

Table1 - Data
[PK_ID] [Name]
1 Joey's Savings
2 Gerry's Savings
3 Dominic's Savings

Is that correct?

Plamen, your new solution doesn't seem to work when I run it in SQL 2005 SP3
or SQL2008 SP1. I think it's because no update statement can update the
same row in a table more than once. (The same problem that shows up with
Microsoft's extension to the UPDATE statement allowing

UPDATE <table1> FROM <table1 INNER JOIN <table2> ON ...

and the inner join has more than one match.)

Julian, one way you could do it is with dynamic SQL. For example, use FOR
XML PATH('') to load a nvarchar variable an update statement with embedded
REPLACE functions - In your example it would look like

Update Data Set Name = Replace(Replace(Name, 'Steven''s', 'Joey''s'),
'Bank', 'Savings);

and then use dynamic sql to execute that statement. Doing this may expose
you to SQL injection attacks. It should be very efficient, but, depending
on how entries get placed into ReplacementCriteria, you need to check very
carefully for SQL injection. If you want to do this way, then it could look
like

Declare @Sql nvarchar(MAX);

With cte As (
Select (Select 'Replace('
From ReplacementCriteria
For XML Path('')) As Replaces,
(Select ', ''' + Replace(search_for, '''', '''''') + ''', '''+
Replace(replace_with, '''', '''''') + ''')'
From ReplacementCriteria
Order By keycol
For XML Path('')) As ReplaceList
)
Select @Sql = 'Update Data Set name = '
+ Cast(Replaces As nvarchar(max))
+ 'name'
+ Cast(ReplaceList As nvarchar(max))
From cte;

Exec sp_executesql @Sql;


You didn't say how many rows will be in ReplacementCriteria. But you may
want to consider just doing this with a cursor. Writing a cursor to do this
is straight-forward, and easy to maintain. The problem with cursors, as you
know, is that they are often slow. Sometimes, however, they are the best
solution. If you only have a few rows in ReplacementCriteria, and/or this
is a one time or a very rare process, the cursor may be an acceptable
solution.

Tom

"Plamen Ratchev" <Plamen@xxxxxxxxxxxxx> wrote in message
news:u3yf7MOAKHA.3708@xxxxxxxxxxxxxxxxxxxxxxx
It is really not clear what you mean, it may be best to illustrate with
example. If you are saying that there should not be a join on the key
column, then simply change that to a cross join to apply to all rows:

WITH UpdateCTE AS (
SELECT name, REPLACE(name, search_for, replace_with) AS new_name
FROM Data AS D
CROSS JOIN ReplacementCriteria AS R)
UPDATE UpdateCTE
SET name = new_name;

--
Plamen Ratchev
http://www.SQLStudio.com

.



Relevant Pages

  • Re: New concurrency error
    ... exception in the condition you specify. ... Person A reads record 1 with timestamp 333 ... Person B issues the update statement. ... when I used to use timestamp for concurrency (SQL 6.5), ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SqlCeCommand.ExecuteNonquery() for update hangs application
    ... If appointmentReason is a bit field, ... SQL CE/SQL Mobile is definitely related to the ... > connection and the update statement and just call ...
    (microsoft.public.sqlserver.ce)
  • Re: Weird problem: Update statement updating records it should not
    ... from table2, table3 ... Field3 won't accept nulls and from the above SQL should never be set ... But the error is that field3 can't be set to null. ... Post the EXACT error the ACTUAL update statement is producing, ...
    (comp.databases.oracle.misc)
  • Re: Sending null values in SQL statements
    ... Thanks Bob. ... asp page and it works well: ... SQL is the UPDATE statement. ...
    (microsoft.public.inetserver.asp.db)
  • Re: Text replacing from another table
    ... Yes, if you are on SQL 2000, common table expressions (the WITH syntax) won't work. ... And, yes, the ReplacementCriteria table is global, each row may affect the entire Data table. ... I will try to attatch the table to an SQL 2005 server and hope that it will work. ...
    (microsoft.public.sqlserver.programming)