简体   繁体   English

sql-query,查找并替换为此特定代码

[英]sql-query, find and replace with this particular code

I need to understand the following SQL query and would like to ask if anybody could explain it to me a bit more in detail (like the xml path) as well as update it with a replace element? 我需要了解以下SQL查询,并想问问是否有人可以向我详细解释一下(例如xml路径)以及使用replace元素进行更新? So I want to find all values with BlaBlaBlaBla and replace them with HaHaHaHa instead. 所以我想用BlaBlaBlaBla查找所有值,并用HaHaHaHa代替它们。 At the moment the query is finding all values of BlaBlaBlaBla only. 目前,查询仅查找BlaBlaBlaBla的所有值。

DECLARE @searchstring  NVARCHAR(255)
SET @searchstring = '%BlaBlaBlaBla%'

DECLARE @sql NVARCHAR(max)

SELECT @sql = STUFF((
  SELECT 
    ' UNION ALL SELECT ''' + TABLE_SCHEMA + '.' + TABLE_NAME + ''' AS tbl, ''' + COLUMN_NAME + ''' AS col, [' + COLUMN_NAME + '] AS val' + 
    ' FROM [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] WHERE [' + COLUMN_NAME + '] LIKE ''' + @searchstring + ''''
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE DATA_TYPE in ('nvarchar', 'varchar', 'char', 'ntext') FOR XML PATH('')) ,1, 11, '')

Exec (@sql)

I believe that the XML PATH is a trick to get the strings to all concatenate together. 我相信XML PATH是一种将所有字符串串联在一起的技巧。

You could change it to REPLACE with something like this: 您可以将其更改为REPLACE ,如下所示:

SELECT @sql = 
SELECT
    ' UPDATE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + '
      SET ' + QUOTENAME(COLUMN_NAME) + ' = REPLACE(' + QUOTENAME(COLUMN_NAME) + ', ''' + @search_string + ''', ' + ', ''' + @replace_string + '''
      WHERE ' + QUOTENAME(COLUMN_NAME) + ' LIKE ''' + @searchstring + ''''
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE
    DATA_TYPE in ('nvarchar', 'varchar', 'char', 'ntext') FOR XML PATH('')

EXEC(@sql)

Some caveats: 一些警告:

I haven't tested this. 我还没有测试。 When you're generating code like this it's very easy to make minor errors with all of the start and end quotes, etc. I would print out the SQL and check it, repeating as necessary until you get the output SQL correct. 当您生成这样的代码时,很容易在所有的引号和结束引号等方面犯一些小错误。我将打印出SQL并进行检查,并根据需要重复进行,直到获得正确的输出SQL。

Also, this is generally not a good idea. 而且,这通常不是一个好主意。 If your database is large and/or has a large number of tables then performance is going to be miserable. 如果您的数据库很大和/或有很多表,那么性能将很糟糕。 You should usually do the analysis of where you think this sort of data is going to appear and write code that will correct it as necessary. 通常,您应该分析您认为这种数据将出现的位置,并编写必要的代码以对其进行纠正。 The fact that data elements are buried in strings throughout your data is concerning. 整个数据中的数据元素都被隐藏在字符串中这一事实令人担忧。

Finally, be aware that this might easily update additional data that you didn't intend to update. 最后,请注意,这可能会轻松更新您不打算更新的其他数据。 If you try to update "123" with "456" and there's a string out there that is "My ID is 1234" it's going to become "My ID is 4564". 如果您尝试用“ 456”更新“ 123”,并且有一个字符串“ My ID is 1234”,它将变为“ My ID is 4564”。

BTW, the QUOTENAME function is a way of enclosing your table and column names in [ and ] , but if the quote character is changed in a DB implementation it should still work. 顺便说一句, QUOTENAME函数是一种将表名和列名括在[] ,但是如果在数据库实现中更改了引号字符,它仍将起作用。

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

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