SQL:根据第二个表替换多行上的部分列
我有一个表 (pages
),它保存网页的属性,其中包括页面内容本身的列,即 NVARCHAR(MAX)
列。
在该列中,我需要查找并替换一堆文本字符串,并将它们替换为其他文本字符串;这些相关性位于第二个表 (moving
) 中,其中包含 oldValue
和 newValue
列。
因此,例如,如果我从这样的两个表开始:
pages table:
ID Content
1 Words words Ancient words
2 Blah blah OutWithTheOld blah
3 Etc etc Useless etc
moving table:
OldValue NewValue
Ancient Better
OutWithTheOld InWithTheNew
Useless Useful
...我需要一种方法来制作替换,留下页表如下:
ID Content
1 Words words Better words
2 Blah blah InWithTheNew blah
3 Etc etc Useful etc
页表中的给定记录可能需要多次替换,并且无法预测页记录是否没有、一个或多个必要的替换,或者来自 的哪些值moving.oldvalue
将被发现并需要被替换。
我正在使用 SQL Server 2008,而且我对它还很陌生。预先非常感谢您提供的任何帮助!
I have a table (pages
) that holds properties for web pages, including a column for the page content itself, an NVARCHAR(MAX)
column.
Within that column, I need to find and replace a bunch of text strings and replace them with other text strings; these correlations are in a second table (moving
), with an oldValue
and newValue
column.
So, for example, if I'm starting with the two tables like this:
pages table:
ID Content
1 Words words Ancient words
2 Blah blah OutWithTheOld blah
3 Etc etc Useless etc
moving table:
OldValue NewValue
Ancient Better
OutWithTheOld InWithTheNew
Useless Useful
...I need a way of making a Replace that leaves the pages table like this:
ID Content
1 Words words Better words
2 Blah blah InWithTheNew blah
3 Etc etc Useful etc
It is possible that a given record in the pages table will need multiple replacements, and there's no way of predicting whether a pages record will have none, one, or many necessary replacements, or which values from moving.oldvalue
will be found and need to be replaced.
I'm using SQL Server 2008, and I'm fairly new to it. Thanks so much in advance for any help you can give!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个使用 CTE 的单语句、非游标方法:
Here's a single-statement, non-cursor method which makes use of a CTE:
试试这个
虽然我通常不喜欢光标,但这应该对你有用
Try this
Although I generally don't like cursors, this should do the trick for you