简体   繁体   English

替换包含表中列的任何结果的字符串的一部分

[英]Replace part of of a string that contains any results from a column in a table

I'm attempting to replace part of a string in a table with specific text if the string contains the exact string from a column in a table. 如果字符串包含表中一列的确切字符串,我尝试用特定文本替换表中字符串的一部分。

I've created a list of just the strings i want to modifying into a variable table, then connecting to the target table with a CROSS JOIN , however the amount of data within the two tables means a cross join could end up coming to hundreds of millions of rows, and the vast majority of the strings i want to swap the data out with will likely not contain any of the data from the second table. 我已经创建了一个只包含要修改为变量表的字符串的列表,然后使用CROSS JOIN连接到目标表,但是两个表中的数据量意味着CROSS JOIN可能最终达到数百个数百万行,而我想与之交换数据的绝大多数字符串将可能不包含第二个表中的任何数据。

There also isn't any method of identifying which records in table 1 will get modified, as there are no identifiers within the string i want to modify. 还没有任何方法可以识别表1中的哪些记录将被修改,因为在我要修改的字符串中没有标识符。 what I have already is below: 我已经在下面的是:

DECLARE @Table2 TABLE (T2.Column1 Varchar(100), T2.Column2 INT)


INSERT INTO @Table2
Select
    T3.Column2 + ', ' + T3.Column3,
    T3.Column1
FROM Table3 T3
    WHERE T3.Column1 IN (1,2,3,4)

UPDATE Table1 SET Column1 = REPLACE(T1.Column1, T2.Column1, 'String')
        FROM Table1 T1
        CROSS JOIN @Table2 T2

I've also attempted the same thing using a CROSS APPLY instead of a CROSS JOIN , but the performance remains the same. 我还尝试使用CROSS APPLY而不是CROSS JOIN进行相同的操作,但是性能保持不变。

This does work, however the performance is poor due to having to join every row on both tables. 确实可以,但是由于必须将两个表的每一行都连接在一起,因此性能很差。 Is there any method where i can achieve the same result but without joining every row to compare? 有什么方法可以使我达到相同的结果,但无需加入每一行进行比较?

EDIT: 编辑:

sample data: 样本数据:

CREATE TABLE Table1 ([Column1] varchar(4000), [Column2] INT)
CREATE TABLE Table3 ([Column1] INT, [Column2] Varchar(50), [Column3] Varchar(50))

INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To Replace 1: text, example', 1)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To Replace 2 example text', 2)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To Replace 3', 3)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To, Replace 4 extra text', 4)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To, Replace 5', 5)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example Data To Replace 6', 6)
INSERT [dbo].[Table1] ([Column1], [Column2]) VALUES ('Example, Data To Replace 7', 7)

INSERT [dbo].[Table3] ([Column1], [Column2], [Column3]) VALUES (1, 'text', 'example')
INSERT [dbo].[Table3] ([Column1], [Column2], [Column3]) VALUES (2, 'To', 'Replace')
INSERT [dbo].[Table3] ([Column1], [Column2], [Column3]) VALUES (3, 'Example', 'Data')

I would have expected it to return the following, but its only actually corrected the first line in table1: 我本来希望它返回以下内容,但它实际上仅纠正了table1中的第一行:

Column1                                     Column2
Example Data To Replace 1: String           1
Example Data To Replace 2 example text      2
Example Data To Replace 3                   3
Example Data String 4 extra text            4
Example Data String 5                       5
Example Data To Replace 6                   6
String To Replace 7                         7

I don't understand why you use cross join when you can use inner join instead: 我不明白为什么可以使用内部联接来代替使用交叉联接:

UPDATE T1 
SET T1.Column1 = REPLACE(T1.Column1, T2.Column1, 'String')
FROM Table1 As T1 
JOIN Table2 As T2 ON T1.Column1 LIKE '%'+ T2.Column1 +'%'

This will only update the records where there is actually something to update, and should have much better performance then a cross join. 这只会更新实际上有待更新的记录,并且其性能要比交叉联接好得多。 If it still suffers a performance issue, you might want to use full text search instead of like. 如果仍然遇到性能问题,则可能要使用全文搜索,而不要使用like。

You can see a live demo on rextester. 您可以在rextester上观看现场演示。

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

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