FROM OPENXML 已过时,不应再使用...(存在罕见的例外情况)。更好地使用真正的 XML 方法。试试这个:
我假设 XML 的上半部分是 meta-data,<voter>elements 是 1:n 相关的work-data。因此,我的结果返回每个选民一行。我希望我没弄错!
DECLARE @DummyTable TABLE(YourXML XML);
INSERT INTO @DummyTable VALUES
(
N'<roll where="house" session="114" year="2015" roll="705" source="house.gov" datetime="2015-12-18T09:49:00-05:00" updated="2016-12-25T10:03:32-05:00" aye="316" nay="113" nv="5" present="0">
<category>passage</category>
<type>On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566</type>
<question>On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566: H R 2029 Making appropriations for military construction, the Department of Veterans Affairs, and related agencies for the fiscal year ending September 30, 2016, and for other purposes</question>
<required>1/2</required>
<result>Passed</result>
<bill session="114" type="h" number="2029" />
<option key="+">Yea</option>
<option key="-">Nay</option>
<option key="P">Present</option>
<option key="0">Not Voting</option>
<voter id="412607" vote="+" value="Yea" state="NC" />
<voter id="400004" vote="+" value="Yea" state="AL" />
<voter id="412615" vote="+" value="Yea" state="CA" />
<voter id="412625" vote="+" value="Yea" state="GA" />
</roll>'
);
--查询
SELECT --first level attributes
r.value(N'@where',N'nvarchar(max)') AS roll_where
,r.value(N'@session',N'int') AS roll_session
,r.value(N'@updated',N'datetime') AS roll_updated
--first level elements
,r.value(N'(category)[1]',N'nvarchar(max)') AS category
,r.value(N'(type)[1]',N'nvarchar(max)') AS [type]
,r.value(N'(result)[1]',N'nvarchar(max)') AS result
--second level attributes
,r.value(N'(bill/@session)[1]',N'int') AS bill_session
--named choice
,r.value(N'(option[@key="+"])[1]',N'nvarchar(max)') AS option_plus
,r.value(N'(option[@key="-"])[1]',N'nvarchar(max)') AS option_minus
,r.value(N'(option[@key="P"])[1]',N'nvarchar(max)') AS option_P
,r.value(N'(option[@key="0"])[1]',N'nvarchar(max)') AS option_0
--it seems to be, that your "voter" elements are a 1:n related list?
--therefore I used a second CROSS APPLY with .nodes() to get them row-wise
,v.value(N'@id','int') AS voter_id
,v.value(N'@vote','nvarchar(max)') AS voter_vote
,v.value(N'@value','nvarchar(max)') AS voter_value
,v.value(N'@state','nvarchar(max)') AS voter_state
FROM @DummyTable AS dt
CROSS APPLY dt.YourXML.nodes(N'roll') AS A(r)
CROSS APPLY A.r.nodes(N'voter') AS B(v)
结果
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+
| roll_where | roll_session | roll_updated | category | type | result | bill_session | option_plus | option_minus | option_P | option_0 | voter_id | voter_vote | voter_value | voter_state |
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+
| house | 114 | 2016-12-25 15:03:32.000 | passage | On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566 | Passed | 114 | Yea | Nay | Present | Not Voting | 412607 | + | Yea | NC |
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+
| house | 114 | 2016-12-25 15:03:32.000 | passage | On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566 | Passed | 114 | Yea | Nay | Present | Not Voting | 400004 | + | Yea | AL |
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+
| house | 114 | 2016-12-25 15:03:32.000 | passage | On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566 | Passed | 114 | Yea | Nay | Present | Not Voting | 412615 | + | Yea | CA |
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+
| house | 114 | 2016-12-25 15:03:32.000 | passage | On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566 | Passed | 114 | Yea | Nay | Present | Not Voting | 412625 | + | Yea | GA |
+------------+--------------+-------------------------+----------+--------------------------------------------------------------------------------+--------+--------------+-------------+--------------+----------+------------+----------+------------+-------------+-------------+