sql XML處理,sp_xml_preparedocument,openxml

來源:互聯網
上載者:User

目的通過解析 XML 擷取
QuestionID,QuestionTypeID,OptionText
SP:
create proc dbo.TestSaveSurvey
(
@XML ntext
)
as
begin
   declare @doc int
   declare @nError int
    set @nError=50001
    --sp_xml_preparedocument要讀取的XML文檔時@XML,
   exec sp_xml_preparedocument @doc output,@xml
   --通過OpenXML擷取行集視圖,
   --/UIResponse/SurveyDetails/s/p/q/o 表示我要出來的節點
    select * into #NullOptionTemp from openxml(@doc,'/UIResponse/SurveyDetails/s/p/q/o',1)
              /*
              QuestionID 列名稱
              int 資料類型
              ../@QuestionId:Xpath,將那些節點映射到列
              */
              with
              (
               QuestionId int '../@QuestionId',
               QuestionTypeId int '../@QuestionTypeId',
               OptionText nvarchar(100) '@OptionText'
              ) 
              where OptionText is null and QuestionTypeid in(2,3,4,5,6,10)
              if exists(select * from #NullOptionTemp)
              begin
                  set @nError=50003
                  goto error
              end
              delete from #NullOptionTemp
     error:
          exec sp_xml_removedocument @doc
        return @nError
end

Exec TestSaveSurvey '

<?xml-stylesheet type=''text/xsl'' href=''styles/GenericPage.xslt''?>
<UIResponse Page="Survey" Action="Edit">
    <UserInfo>
        <u UserId="1304" UserLoginName="fareast\v-guohu" UserFullName="Guo Hu">
            <r RoleId="1"/>
        </u>
    </UserInfo>
    <SurveyDetails>
        <s SurveyId="2253" SurveyName="The Questions have no options in the survey" SurveyDescription="The Questions have no options in the survey description" CategoryId="0" LanguageId="6" StatusId="11" SurveyFriendlyName="question_have_no_options" SurveyTitle="The Questions have no options in the survey" IntroductionText="" ThankYouText="" GraphicUrl="" GraphicAltText="" RequiredExplanationText="" RequiredErrorText="" PreviousButtonText="" NextButtonText="" CloseButtonText="Close" SkipButtonText="Skip" FinishButtonText="Finish" CancelButtonText="Cancel" XslTemplatePath="" AcceptButtonText="Accept" DeclineButtonText="" OptOutText="" TrackingText="" ExpireTime="0" Updatable="1" Publishable="1" AnalyticsReadable="1" SurveySiteName="GN">
            <p PageId="132" PageName="Page: -1" NextPageId="0">
                <q QuestionId="13111" QuestionName="test question" QuestionTypeId="3" QuestionRequiredFlag="0" QuestionText="Do you like to have this survey?" VariableName="" Randomization="0" NumberOfColumns="0" DontKnowFlag="0">
                    <o OptionId="19948" OptionText="like it!" NonSelect="0" OptionValue="0" VariableName="" PipingVariableName="" Fixed="0" Exclusive="0"/>
                    <o OptionId="19949" OptionText="Don''t like it!" NonSelect="0" OptionValue="1" VariableName="" PipingVariableName="" Fixed="0" Exclusive="0"/>
                </q>
                <q QuestionId="13117" QuestionName="new radio question" QuestionTypeId="3" QuestionRequiredFlag="0" QuestionText="" VariableName="" Randomization="0" NumberOfColumns="0" DontKnowFlag="0">
                    <o OptionValue="0" VariableName="" PipingVariableName="" Fixed="0" Exclusive="0"/>
                </q>
            </p>
        </s>
    </SurveyDetails>
</UIResponse>

'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.