我需要编写一个 WHILE 循环并将一个变量传递给一个 xml 查询:
鉴于此逻辑:
DECLARE @count INT = 0;
DECLARE @pointer INT = 0;
DECLARE @pointer_str varchar(100);
DECLARE @MyXML XML
SET @MyXML = '<SampleXML>
<Colors>
<Color1>White</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Colors>
<Color1>White</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
</SampleXML>'
SET @count =
(
SELECT
a.b.value('count(/SampleXML/Colors)', 'int')
FROM
@MyXML.nodes('data') a(b)
)
WHILE @count < @pointer
BEGIN
SELECT
a.b.value('Colors[' + @pointer + ']/Color1[1]','varchar(10)') AS Color1
FROM
@MyXML.nodes('SampleXML') a(b);
SELECT @pointer = @pointer + 1;
END
我试图在 SELECT 语句中获取以下变量来解析 XML,但收到以下错误:
The argument 1 of the XML data type method "value" must be a string literal.
此外,当我将字面值“1”传递给它时,我看不到在循环内运行的选择。有人可以帮帮我吗?