在SQL Server

时间:2016-06-13 16:13:23

标签: sql-server-2008

我有以下格式的SQL Server数据:

DECLARE @tblData TABLE 
(
     ID int,
     texttofind varchar(2000),
     txttoReplace varchar(2000),
     Body varchar(max)
)

INSERT INTO @tblData 
VALUES (1,'url1','someurl1','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>')

INSERT INTO @tblData 
VALUES (1,'url2','someurl2','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>')

INSERT INTO @tblData 
VALUES (1,'url3','someurl3','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>')

INSERT INTO @tblData 
VALUES (2,'url1','someotherurl1','<href="url1">test</a><href="url2">test2</a>')

INSERT INTO @tblData 
VALUES (2,'url2','someotherurl2','<href="url2">test</a><href="url2">test2</a>')


SELECT * FROM @tblData


DECLARE @tblExpectedData TABLE (ID int,
                                texttofind varchar(2000),
                                txttoReplace varchar(2000),
                                Body varchar(max),
                                BodyAfterChange varchar(max)
                               )

INSERT INTO @tblExpectedData 
VALUES (1,'url1','someurl1','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>','<href="someurl1">test</a><href="someurl2">test2</a><href="someurl3">test3</a>')

INSERT INTO @tblExpectedData 
VALUES (1,'url2','someurl2','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>','<href="someurl1">test</a><href="someurl2">test2</a><href="someurl3">test3</a>')

INSERT INTO @tblExpectedData 
VALUES (1,'url3','someurl3','<href="url1">test</a><href="url2">test2</a><href="url3">test3</a>','<href="someurl1">test</a><href="someurl2">test2</a><href="someurl3">test3</a>')

INSERT INTO @tblExpectedData 
VALUES (2,'url1','someotherurl1','<href="url1">test</a><href="url2">test2</a>','<href="someotherurl1">test</a><href="someotherurl2">test2</a>')

INSERT INTO @tblExpectedData 
VALUES (2,'url2','someotherurl2','<href="url2">test</a><href="url2">test2</a>','<href="someotherurl1">test</a><href="someotherurl2">test2</a>')

SELECT * FROM @tblExpectedData

第一个表是数据的样子,第二个表是我想要的预期结果,基本上是从&#34; texttofind&#34;中进行查找和替换。到&#34;身体&#34;使用&#34; txttoReplace&#34;列,但我无法弄清楚如何为每个ID和每一行执行此操作,以便特定ID的每一行都具有相同的&#34; BodyAfterChange&#34;

0 个答案:

没有答案