注意:See this spelled out example on SQL Fiddle, or look at the code below:,因为DECLARE @XML variable
示例在尝试从XML列实际获取XML数据并且用于非XML行时会混淆语法:
CREATE TABLE ##xml (
ID TINYINT IDENTITY(1,1),
Value XML
)
INSERT INTO ##xml (Value)
VALUES ('<Animals key="zoo" fish="22" dogs="0" birds="4" />')
, ('<Animals key="house" fish="0" dogs="1" birds="2" />')
, ('<Animals key="business" fish="0" dogs="0" birds="12" />')
SELECT *
FROM ##xml
SELECT nodes.child.value('key[1]', 'VARCHAR(50)')
FROM ##xml.Value.nodes('Animal') AS nodes(child)
-- Errors here, though the syntax looks correct
DROP TABLE ##xml
我收到错误,“无效的列名'## xml'。只能在类型为xml的列上调用XMLDT方法'节点'”即使我正在尝试查询XML的节点以生成结果如下:
Key | Fish | Dogs | Birds
Zoo 22 0 4
House 0 1 2
Business 0 0 12
注意,如果我将FROM语句的语法更改为指向特定列(Value),则会收到有关Value不是可识别的内置函数名称的其他错误。
答案 0 :(得分:4)
这样做:
SELECT nodes.child.value('@key','varchar(100)')
FROM ##XML a
CROSS APPLY a.value.nodes('/Animals') AS nodes(child)