开发者

exec sp_xml_preparedocument with external column

I have this table

declare  @temp_xml table
(
    question_id int,
    question_xml xml
)

insert into @temp_xml(question_id, question_xml)
values(51, '<qst qodm="Horizo开发者_开发知识库ntal" 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

Now I want to show like this .

   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.

any one can help me out ?

Thanks.


I would avoid the old openxml API altogether and use the new XQuery style in SQL Server 2005 and newer.

In your case, this would be something like:

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)

This produces an output like this (I included the "QVID" column, just to show what really gets selected):

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜