表a:
+------+------+------+--------------------------------------+
| col1 | col2 | col3 | Col4 |
+------+------+------+--------------------------------------+
| 1 | A | E |<?xml version .....<v> "OOOO"</v></vs>|
| 2 | B | F |<?xml version .....<v> "KKKK"</v></vs>|
| 3 | C | G |<?xml version .....<v> "LLLL"</v></vs>|
| 4 | B | E |<?xml version .....<v> "MMMM"</v></vs>|
| 5 | T | G |<?xml version .....<v> "NNNN"</v></vs>|
+------+------+------+--------------------------------------+
如何在“”?
之间更新xml以下是我的尝试,这导致将col4更新为NewValue 1 - x。基本上它不保留xml格式。
UPDATE sf
SET sf.col4 =
(CASE col4
WHEN '%OOOO%'
THEN '%NewValue1%'
WHEN '%KKKK%'
THEN '%NewValue2%'
WHEN '%LLLL%'
THEN '%NewValue3%'
WHEN '%MMMM%'
THEN '%NewValue4%'
WHEN '%NNNN%'
THEN '%NewValue5%'
END)
预期结果:
+------+------+------+-------------------------------------------+
| col1 | col2 | col3 | Col4 |
+------+------+------+-------------------------------------------+
| 1 | A | E |<?xml version .....<v> "NewValue1"</v></vs>|
| 2 | B | F |<?xml version .....<v> "NewValue2"</v></vs>|
| 3 | C | G |<?xml version .....<v> "NewValue3"</v></vs>|
| 4 | B | E |<?xml version .....<v> "NewValue4"</v></vs>|
| 5 | T | G |<?xml version .....<v> "NewValue5"</v></vs>|
+------+------+------+-------------------------------------------+
Col4是数据类型:nvarchar(MAX)
答案 0 :(得分:3)
使用Replace
你可以试试这个: -
UPDATE sf
SET sf.col4 = ( CASE
WHEN col4 like '%OOOO%' THEN Replace(Col4, 'OOOO', 'NewValue1')
WHEN col4 like '%KKKK%' THEN Replace(Col4, 'KKKK', 'NewValue2')
WHEN col4 like '%LLLL%' THEN Replace(Col4, 'LLLL', 'NewValue3')
WHEN col4 like '%MMMM%' THEN Replace(Col4, 'MMMM', 'NewValue4')
WHEN col4 like '%NNNN%' THEN Replace(Col4, 'NNNN', 'NewValue5')
END )
答案 1 :(得分:1)
如果您想查看使用XML功能的解决方案:
DECLARE @tbl TABLE(col1 INT,col2 VARCHAR(10),col3 VARCHAR(10),Col4 XML);
INSERT INTO @tbl VALUES
(1,'A','E','<?xml version="1.0"?><vs><v> "OOOO"</v></vs>')
,(2,'B','F','<?xml version="1.0"?><vs><v> "KKKK"</v></vs>')
,(3,'C','G','<?xml version="1.0"?><vs><v> "LLLL"</v></vs>')
,(4,'B','E','<?xml version="1.0"?><vs><v> "MMMM"</v></vs>')
,(5,'T','G','<?xml version="1.0"?><vs><v> "NNNN"</v></vs>');
SELECT * FROM @tbl;
/*
1 A E <vs><v> "OOOO"</v></vs>
2 B F <vs><v> "KKKK"</v></vs>
3 C G <vs><v> "LLLL"</v></vs>
4 B E <vs><v> "MMMM"</v></vs>
5 T G <vs><v> "NNNN"</v></vs>
*/
现在您查询并更新:
WITH MyData AS
(
SELECT tbl.*
,tbl.Col4.value('(/vs/v)[1]','varchar(max)') AS ContentV
FROM @tbl AS tbl
)
,ExtendedWithNewValue AS
(
SELECT *
,CASE WHEN ContentV LIKE '%OOOO%' THEN 'NewValue1'
WHEN ContentV LIKE '%KKKK%' THEN 'NewValue2'
WHEN ContentV LIKE '%LLLL%' THEN 'NewValue3'
WHEN ContentV LIKE '%MMMM%' THEN 'NewValue4'
WHEN ContentV LIKE '%NNNN%' THEN 'NewValue5'
ELSE ContentV
END AS NewValue
FROM MyData
)
UPDATE @tbl
SET col4.modify('replace value of (/vs/v/text())[1] with sql:column("nv.NewValue")')
FROM @tbl AS tbl INNER JOIN ExtendedWithNewValue AS nv ON tbl.col1=nv.col1;
SELECT * FROM @tbl;
/*
col1 col2 col3 Col4
1 A E <vs><v>NewValue1</v></vs>
2 B F <vs><v>NewValue2</v></vs>
3 C G <vs><v>NewValue3</v></vs>
4 B E <vs><v>NewValue4</v></vs>
5 T G <vs><v>NewValue5</v></vs>
*/