0

我需要将以下 XML 导入 SQL Server 2008 R2:

<Role
  ID="ef63f979-c65d-421a-a214-7319c6279e13" >
  <Privileges>
    <Privilege ID="1" />
    <Privilege ID="2" />
    <Privilege ID="3" />
    <Privilege ID="4" />
    <Privilege ID="5" />
  </Privileges>
</Role>

我可以使用表变量来执行此操作并解析 XML 两次,但无法在一次解析中将其放入关系表中。那可能吗?这是预期的结果集:

EF63F979-C65D-421A-A214-7319C6279E13    1   2011-12-05 06:51:11.9065096 EF63F979-C65D-421A-A214-7319C6279E13
EF63F979-C65D-421A-A214-7319C6279E13    2   2011-12-05 06:51:11.9065096 EF63F979-C65D-421A-A214-7319C6279E13
EF63F979-C65D-421A-A214-7319C6279E13    3   2011-12-05 06:51:11.9065096 EF63F979-C65D-421A-A214-7319C6279E13
EF63F979-C65D-421A-A214-7319C6279E13    4   2011-12-05 06:51:11.9065096 EF63F979-C65D-421A-A214-7319C6279E13
EF63F979-C65D-421A-A214-7319C6279E13    5   2011-12-05 06:51:11.9065096 EF63F979-C65D-421A-A214-7319C6279E13

谢谢,

拉吉

4

1 回答 1

0

我可以提取这个:

DECLARE @input XML = '<Role
  ID="ef63f979-c65d-421a-a214-7319c6279e13" >
  <Privileges>
    <Privilege ID="1" />
    <Privilege ID="2" />
    <Privilege ID="3" />
    <Privilege ID="4" />
    <Privilege ID="5" />
  </Privileges>
</Role>'

SELECT
    @input.value('(/Role/@ID)[1]', 'uniqueidentifier') AS 'ID',
    T.C.value('(@ID)[1]', 'int') AS 'Privilege'
FROM
    @input.nodes('/Role/Privileges/Privilege') AS T(C)

给出输出:

ID                                     Privilege
EF63F979-C65D-421A-A214-7319C6279E13    1
EF63F979-C65D-421A-A214-7319C6279E13    2
EF63F979-C65D-421A-A214-7319C6279E13    3
EF63F979-C65D-421A-A214-7319C6279E13    4
EF63F979-C65D-421A-A214-7319C6279E13    5

但我不知道你的日期/时间来自哪里......

于 2011-12-05T07:16:39.053 回答