2017-10-10 68 views
1

我已經編寫了一個存儲過程,它將通過xml進行查詢,並根據傳遞給它的Xpath返回值。我給了一個虛擬的XML數據來調試。存儲過程被創建,但是當我調試它時,它說必須聲明標量變量@xml。我不知道我要去哪裏錯。在SQL Server中使用Xpath

我在分享存儲過程代碼以獲取更多參考。

spXML 
'Gender','''/CreateAndSendMessageRequest/CompositionRequest/PolicyDetails 
/MainPolicyHolder/PolicyHolderDetails''' 

ALTER PROCEDURE spXML 
    @lasttag VARCHAR(50), 
    @root VARCHAR(MAX) 
AS 
BEGIN 
    Declare @xml xml 
    Set @xml = '<?xml version="1.0" encoding="utf-8"?> 
       <CreateAndSendMessageRequest> 
        <CompositionRequest> 
         <Metadata> 
          <PolicyReference>250028766622DN</PolicyReference> 
          <AccountReference>Test1234</AccountReference> 
          <QuoteReference>Test3214</QuoteReference> 
          <OutboundTransactionID>string</OutboundTransactionID> 
          <InboundActivityID>string</InboundActivityID> 
          <DocumentName>DocumentA127</DocumentName> 
          <DocumentID /> 
          <CommunicationID>C004</CommunicationID> 
          <CorrelationID>PC: 20c14f9b-2a1b-45dc-b680-52ffceb86d05</CorrelationID> 
          <ContentType>string</ContentType> 
          <isSensitive>true</isSensitive> 
          <isReadOnly>true</isReadOnly> 
          <isDocumentInbound>true</isDocumentInbound> 
          <ChannelIdentifier>POST</ChannelIdentifier> 
          <BrandType>string</BrandType> 
          <SchemeNameCode>string</SchemeNameCode> 
          <isNotificationRequiredIndicator>false</isNotificationRequiredIndicator> 
          <DocumentPriorityIndicator>Standard</DocumentPriorityIndicator> 
          <SpecialInstructions> 
           <Code>string</Code> 
           <Description>string</Description> 
          </SpecialInstructions> 
          <SupressIndicator>false</SupressIndicator> 
          <OutsortIndicator>string</OutsortIndicator> 
          <DocumentIncludes> 
           <DocumentId DocumentTitle="string">string</DocumentId> 
          </DocumentIncludes> 
          <CreatedBy>string</CreatedBy> 
          <UpdatedBy>string</UpdatedBy> 
          <FulfilledDateTime>2017-05-15T07:41:13</FulfilledDateTime> 
          <UploadedDateTime>2013-12-21T17:02:42+05:30</UploadedDateTime> 
          <RequestDateTime>2016-01-01T19:07:42</RequestDateTime> 
          <UpdateDateTime>2012-01-07T19:42:56</UpdateDateTime> 
         </Metadata> 
         <PolicyDetails> 
          <PolicyReference>string</PolicyReference> 
          <QuoteReference>string</QuoteReference> 
          <PolicyVersionStartDate>2009-11-03</PolicyVersionStartDate> 
          <MainPolicyHolder> 
           <PolicyHolderDetails> 
            <Title>string</Title> 
            <Initials>string</Initials> 
            <Honours>string</Honours> 
            <Firstname>string</Firstname> 
            <Surname>string</Surname> 
            <FullName>string</FullName> 
            <Gender>F</Gender> 
            <DateOfBirth>2018-04-13</DateOfBirth> 
           </PolicyHolderDetails> 
          </PolicyDetails> 
         </CompositionRequest> 
         <VersionInfo majorVersion="1" minorVersion="19" /> 
        </CreateAndSendMessageRequest>' 


    Declare @tag varchar(max) 

    set @tag = 'Select X.T.value(''('[email protected]+')[1]'',''varchar(50)'') as Result 
       From @xml.nodes('[email protected]+') as X(T)' 

    print @tag 

    Execute(@tag) 
End 
+0

如果刪除'@ xml'的大部分內容,你會得到同樣的錯誤嗎?我想知道,如果這是一個字符串格式問題,你有一個額外的'''在xml的地方(很難看到與當前的格式) – DiskJunky

+1

我已編輯它相應 – Ksingh

回答

1

您嘗試運行像這樣

Declare @tag varchar(max) 
set @tag = 'Select X.T.value(''('[email protected]+')[1]'',''varchar(50)'')as 
Result 
From @xml.nodes('[email protected]+') as X(T)' 
print @tag 
Execute(@tag) 

即引用其範圍以外的變量即席查詢。 @xml是存儲過程中的變量,而不是動態查詢select X.T.value...中的變量。您需要將@xml放入臨時查詢中才能使用它。

+0

我也試過,但..它會返回相同的錯誤。 @DiskJunky – Ksingh

+0

@Ksingh你能發表你試過的嗎?在更新 – DiskJunky

+0

可能會有另一個範圍問題我只是通過'@xml'的地方,我宣佈其他變量,如'lasttag'和'@root' – Ksingh