DECLARE @XmlData xml
SET @XmlData= '<?xml version="1.0" encoding="utf-8" ?>
<ROOT PROCESS_DATE="25-Nov-2009" ROW_COUNT="2" VERIFY_TOTAL="654454.54">
<row rowNumber="1">
<Code1>11111</Code1>
<Code2>AAAA </Code2>
</row>
<row rowNumber="2">
<Code1>2222</Code1>
<Code2>BBBB </Code2>
</row>
</ROOT>'
-- Following query returns the Code1, & Code2 for each row.
SELECT
R.i.query('Code1').value('.', 'nvarchar(8)') AS Code1,
R.i.query('Code2').value('.', 'nvarchar(8)') AS Code2
FROM @XmlData.nodes('/ROOT/row') R(i)
-- Now I want to get the rowNumber attribute of each row in the resultset.
SELECT
R.i.query('@rowNumber') AS rowNumber,
R.i.query('Code1').value('.', 'nvarchar(8)') AS Code1,
R.i.query('Code2').value('.', 'nvarchar(8)') AS Code2
FROM @XmlData.nodes('/ROOT/row') R(i)
-- Above query returns error
问问题
293 次
3 回答
2
DECLARE @x XML ;
SET @x = '<?xml version="1.0" encoding="utf-8" ?>
<ROOT PROCESS_DATE="25-Nov-2009" ROW_COUNT="2" VERIFY_TOTAL="654454.54">
<row rowNumber="1">
<Code1>11111</Code1>
<Code2>AAAA </Code2>
</row>
<row rowNumber="2">
<Code1>2222</Code1>
<Code2>BBBB </Code2>
</row>
</ROOT>' ;
SELECT v.value('.', 'VARCHAR(20)') AS Name
FROM @x.nodes('(/ROOT/row/@*)') x ( v )
于 2009-12-15T07:48:39.980 回答
0
您可以使用解决方法查询属性data()
:
R.i.query('data(row/@Name)').value('.','int') AS rowNumber
于 2009-11-25T13:49:50.813 回答
0
我手边没有 SQL Server,但我的直觉告诉我要尝试:
R.i.query('.').value('@rowNumber', 'int') AS rowNumber
于 2009-11-25T13:16:02.317 回答