exec sp_xml_preparedocument with external column

时间:2011-08-20 08:22:01

标签: sql-server

我有这张桌子

declare  @temp_xml table
(
    question_id int,
    question_xml xml
)

insert into @temp_xml(question_id, question_xml)
values(51, '<qst qodm="Horizontal" oprm="New Line" oph="25" opw="200" stext=""   filepath="" peqid="-1" peaid="-1" pipeqtype="1" rad="False"><cps><qvs qvid="V1" qvt="Required" qvem="Please choose at least one answer." /><qvs qvid="V15" qvt="UK Phone Number " qvem="Is not a correct phone format." /></cps><branch><![CDATA[]]></branch></qst>'
      ),
      (52,
   '<qst qodm="Horizontal" oprm="New Line" oph="25" opw="200" stext="" filepath="" peqid="-1" peaid="-1" pipeqtype="1" rad="False"><cps><qvs qvid="V1" qvt="Required" qvem="Please choose at least one answer." /><qvs qvid="V5" qvt="US/Canada  Phone Number " qvem="Is not a correct phone format." /></cps><branch><![CDATA[]]></branch></qst>'
      )

select * from @temp_xml

现在我想要这样展示。

   Question_id  qvt                       qvem
   51,          Required,                 Please choose at least one answer.
   51,          UK Phone Number,          Is not a correct phone format.
   52,          Required,                 Please choose at least one answer.
   52,          US/Canada  Phone Number,  Is not a correct phone format.

任何人都可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我会完全避免使用旧的openxml API并在SQL Server 2005及更新版本中使用新的XQuery样式。

在您的情况下,这将是:

DECLARE @Temp_XML TABLE (question_id int, question_xml xml)

insert into @Temp_XML(question_id, question_xml)
values(51, 
'<qst qodm="Horizontal" oprm="New Line" oph="25" opw="200" stext="" filepath="" peqid="-1" peaid="-1" pipeqtype="1" rad="False">
    <cps>
        <qvs qvid="V1" qvt="Required" qvem="Please choose at least one answer." />
        <qvs qvid="V15" qvt="UK Phone Number " qvem="Is not a correct phone format." />
    </cps>
    <branch><![CDATA[]]></branch>
</qst>'),
      (52,
'<qst qodm="Horizontal" oprm="New Line" oph="25" opw="200" stext="" filepath="" peqid="-1" peaid="-1" pipeqtype="1" rad="False">
    <cps>
        <qvs qvid="V1" qvt="Required" qvem="Please choose at least one answer." />
        <qvs qvid="V5" qvt="US/Canada  Phone Number " qvem="Is not a correct phone format." />
    </cps>
    <branch><![CDATA[]]></branch>
</qst>')

SELECT
    question_id,
    CPS.QVS.value('(@qvid)[1]', 'varchar(50)') AS 'QVID',
    CPS.QVS.value('(@qvt)[1]', 'varchar(50)') AS 'QuestionValidationType',
    CPS.QVS.value('(@qvem)[1]', 'varchar(50)') AS 'QuestionValidationMessage'
FROM  
    @Temp_xml
CROSS APPLY
    question_xml.nodes('/qst/cps/qvs') AS CPS(QVS)

这会产生这样的输出(我包括“QVID”列,只是为了显示真正被选中的内容):

question_id QVID  QuestionValidationType     QuestionValidationMessage
51           V1     Required                 Please choose at least one answer.
51           V15    UK Phone Number          Is not a correct phone format.
52           V1     Required                 Please choose at least one answer.
52           V5     US/Canada Phone Number   Is not a correct phone format.