2017-10-10 102 views
0

是否有人使用SOAP格式的SQL Server生成XML文件?我試圖從SQL生成以下結構,但如果FOR XML子句是最佳方法,我無法弄清楚。任何人都可以提一些建議嗎也許這種文件的另一種方法?由於使用SQL Server生成SOAP XML

SOAP XML結構

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
     <To s:mustUnderstand="1" xmlns=""></To> 
     <Action s:mustUnderstand="1" xmlns=""></Action> 
    </s:Header> 
    <soapenv:Body> 
     <tns:StoreDocument xmlns:tns=""> 
     <tns:StoreDocumentMessage> 
      <q1:SystemCode xmlns:q1=""></q1:SystemCode> 
      <q1:TransmissionUID xmlns:q1="">d2032705-5478-4694-98f9-d2eafa3933b2</q1:TransmissionUID> 
      <q1:DocumentSection xmlns:q1=""> 
       <q17:DocumentType xmlns:q17="">1</q17:DocumentType> 
       <q17:DocumentNumber xmlns:q17="">0000006965</q17:DocumentNumber> 
       <q17:DocumentUID xsi:nil="true" xmlns:q17="" xmlns:xsi=""/> 
       <q17:CreationDateTimeUTC xmlns:q17="">2017-08-04 09:09:29 -03:00</q17:CreationDateTimeUTC> 
       <q17:DocumentStatus xmlns:q17="">2</q17:DocumentStatus> 
       <q17:LatestGoodIssueDate xmlns:q17="">2017-08-04 13:35:16 -03:00</q17:LatestGoodIssueDate> 
       <q17:Reference xmlns:q17="">0000013254</q17:Reference> 
       <q17:TotalOrderedProducts xsi:nil="true" xmlns:q17="" xmlns:xsi=""/> 
       <q17:OrderedProducts xmlns:q17=""> 
        <q17:OrderedProductSection> 
        <q17:ProductCode>2152157</q17:ProductCode> 
        <q17:OrderedQuantity>500000</q17:OrderedQuantity> 
       </q17:OrderedProducts> 
       <q17:RelatedDocumentSection xsi:nil="true" xmlns:q17="" xmlns:xsi=""/> 
       <q17:StockOwnerSection xmlns:q17=""> 
        <q17:StockOwnerCode></q17:StockOwnerCode> 
        <q17:StockOwnerName></q17:StockOwnerName> 
        <q17:StockOwnerAddress1></q17:StockOwnerAddress1> 
        <q17:StockOwnerAddress2/> 
        <q17:StockOwnerZIP>1407</q17:StockOwnerZIP> 
        <q17:StockOwnerCity></q17:StockOwnerCity> 
        <q17:StockOwnerNameRomanized></q17:StockOwnerNameRomanized> 
        <q17:StockOwnerAddress1Romanized></q17:StockOwnerAddress1Romanized> 
        <q17:StockOwnerAddress2Romanized/> 
        <q17:StockOwnerZIPRomanized></q17:StockOwnerZIPRomanized> 
        <q17:StockOwnerCityRomanized></q17:StockOwnerCityRomanized> 
        <q17:StockOwnerCountry></q17:StockOwnerCountry> 
        <q17:StockOwnerGLN xsi:nil="true" xmlns:xsi=""/> 
       </q17:StockOwnerSection> 
     </tns:StoreDocumentMessage> 
     </tns:StoreDocument> 
    </soapenv:Body> 
</soapenv:Envelope> 

回答

1

就我而言,我能夠做到以下存儲過程。希望會有所幫助!

DECLARE @XMLFINAL VARCHAR(MAX) 
SET @XMLFINAL='' 
DECLARE @nr NVARCHAR(100) 
DECLARE @XML VARCHAR(MAX) 

DECLARE Records CURSOR FAST_FORWARD FOR 

SELECT TOP 10 GID FROM ESFITradeAccount 

OPEN Records 
FETCH NEXT FROM Records INTO @nr 
WHILE @@FETCH_STATUS = 0 
BEGIN 
SET @XML='' 
SET @XML=(
SELECT (

SELECT 
Code, Name 

FROM ESFITradeAccount WHERE [email protected] 
FOR 
XML AUTO, ELEMENTS 
)) 

FETCH NEXT FROM Records INTO @nr 

SET @[email protected][email protected] 
END 

CLOSE Records DEALLOCATE Records 


IF @XMLFINAL IS NOT NULL 
BEGIN  
INSERT INTO EXPORTAEVWMS_Test (XML, Send) 
SELECT @XMLFINAL,'0' 

DECLARE @XMLEXPOPRT VARCHAR(MAX) 
SET @XMLEXPOPRT='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' 
+'<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' 
+'<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://10.2.3.92:8201/FlexibleStorage/Transmission?wsdl</To>' 
+'<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://ITrack.Transmission/2011/02/25/Interfaces/TransmissionServer/StoreDocument</Action>' 
+'</s:Header>' 
+'<soapenv:Body>' 
+'<tns:StoreDocument xmlns:tns="http://ITrack.Transmission/2011/02/25/Interfaces">' 
+'<tns:StoreDocumentMessage>' 
+'<q1:SystemCode xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">ROM_ENTSOFT_PROD</q1:SystemCode>' -- system code 
+'<q1:TransmissionUID xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">'+convert(nvarchar(36), newid())+'</q1:TransmissionUID>' -- generated GID 
+'<q1:DocumentSection xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">' 
+'<q17:DocumentType xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">1</q17:DocumentType>' -- fix value (1) 
+'<q17:DocumentNumber xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">0000006965</q17:DocumentNumber>' -- ADCode of delivery note 
+'<q17:DocumentUID xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- not used 
+'<q17:CreationDateTimeUTC xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'+format((SELECT ESDCreated FROM PurchasesDocuments), 'yyyy-MM-dd HH:mm:ss')+'</q17:CreationDateTimeUTC>' -- timestamp of the order 
+'<q17:DocumentStatus xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">2</q17:DocumentStatus>' -- 1 = Open; 2 = Shipped; 
+'<q17:LatestGoodIssueDate xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'+format(getdate(), 'yyyy-MM-dd HH:mm:ss')+'</q17:LatestGoodIssueDate>' -- the date when the goods are shipped 
+'<q17:Reference xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">0000013254</q17:Reference>' -- invoice number 
+'<q17:TotalOrderedProducts xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- not used 
+'<q17:OrderedProducts xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' -- document lines (RSKU + Quantity) 
[email protected] 
+'</q17:OrderedProducts>' 
+'<q17:RelatedDocumentSection xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- left empty (not used) 
+'<q17:StockOwnerSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' -- the section related to StocOwnerSection contain the address of the market headquarter (ITDR Romania) 
+'<q17:StockOwnerCode>E172</q17:StockOwnerCode>' -- ITDR headquarter 
+'<q17:StockOwnerName>Импириъл Табако Дистрибуция ЕООД</q17:StockOwnerName>' -- company name 
+'<q17:StockOwnerAddress1>ул. Хенрих Ибсен 15, ет.4, офис 4</q17:StockOwnerAddress1>' -- company address1 
+'<q17:StockOwnerAddress2/>' -- company address2 
+'<q17:StockOwnerZIP>1407</q17:StockOwnerZIP>' -- company ZIP code 
+'<q17:StockOwnerCity>София</q17:StockOwnerCity>' -- company city 
+'<q17:StockOwnerNameRomanized>Imperial Tobacco Distribution</q17:StockOwnerNameRomanized>' -- romanian info (company name) 
+'<q17:StockOwnerAddress1Romanized>str. Heinrich Ibsen 15 et.4</q17:StockOwnerAddress1Romanized>' -- romanian info (company address1) 
+'<q17:StockOwnerAddress2Romanized/>' -- romanian info (company address2) 
+'<q17:StockOwnerZIPRomanized>1407</q17:StockOwnerZIPRomanized>' -- romanian info (company ZIP code) 
+'<q17:StockOwnerCityRomanized>SOFIA</q17:StockOwnerCityRomanized>' -- country description of the owner 
+'<q17:StockOwnerCountry>BG</q17:StockOwnerCountry>' -- country code of the owner 
+'<q17:StockOwnerGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- left empty (not used) 
+'</q17:StockOwnerSection>' 
+'<q17:DestinationSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' 
+'<q17:DestinationCode>13400001</q17:DestinationCode>' 
+'<q17:DestinationName>Табако Трейд ООД</q17:DestinationName>' 
+'<q17:DestinationAddress1>Пловдив бул. Христо Ботев 94</q17:DestinationAddress1>' 
+'<q17:DestinationAddress2 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:DestinationZIP>4000</q17:DestinationZIP>' 
+'<q17:DestinationCity>Пловдив</q17:DestinationCity>' 
+'<q17:DestinationNameRomanized>Tobacco Trade LTD</q17:DestinationNameRomanized>' 
+'<q17:DestinationAddress1Romanized>137 Bresovsko shose Str.</q17:DestinationAddress1Romanized>' 
+'<q17:DestinationAddress2Romanized xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:DestinationZIPRomanized>4000</q17:DestinationZIPRomanized>' 
+'<q17:DestinationCityRomanized>Plovdiv</q17:DestinationCityRomanized>' 
+'<q17:DestinationCountry>BG</q17:DestinationCountry>' 
+'<q17:DestinationGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:DestinationCoord xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'</q17:DestinationSection>' 
+'<q17:SoldToSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' 
+'<q17:SoldToCode>13400001</q17:SoldToCode>' 
+'<q17:SoldToName>Табако Трейд ООД</q17:SoldToName>' 
+'<q17:SoldToAddress1>Пловдив бул. Христо Ботев 94</q17:SoldToAddress1>' 
+'<q17:SoldToAddress2 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:SoldToZIP>4000</q17:SoldToZIP>' 
+'<q17:SoldToCity>Пловдив</q17:SoldToCity>' 
+'<q17:SoldToNameRomanized>Tobacco Trade LTD</q17:SoldToNameRomanized>' 
+'<q17:SoldToAddress1Romanized>137 Bresovsko shose Str.</q17:SoldToAddress1Romanized>' 
+'<q17:SoldToAddress2Romanized xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:SoldToZIPRomanized>4000</q17:SoldToZIPRomanized>' 
+'<q17:SoldToCityRomanized>Plovdiv</q17:SoldToCityRomanized>' 
+'<q17:SoldToCountry>BG</q17:SoldToCountry>' 
+'<q17:SoldToGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:SoldToSameGroupAsStockOwner>false</q17:SoldToSameGroupAsStockOwner>' 
+'</q17:SoldToSection>' 
+'<q17:TrackingLocationSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' 
+'<q17:TrackingLocationCode>BG9992</q17:TrackingLocationCode>' 
+'<q17:TrackingLocationName>Импириъл Табако Дистрибуция ЕООД</q17:TrackingLocationName>' 
+'<q17:TrackingLocationAddress1>Ул. Иван Георгов 3</q17:TrackingLocationAddress1>' 
+'<q17:TrackingLocationAddress2>Военна рампа</q17:TrackingLocationAddress2>' 
+'<q17:TrackingLocationZIP>1220</q17:TrackingLocationZIP>' 
+'<q17:TrackingLocationCity>София</q17:TrackingLocationCity>' 
+'<q17:TrackingLocationNameRomanized>Imperial Tobacco Distribution</q17:TrackingLocationNameRomanized>' 
+'<q17:TrackingLocationAddress1Romanized>3 Ivan Georgov Str.</q17:TrackingLocationAddress1Romanized>' 
+'<q17:TrackingLocationAddress2Romanized>Voenna rampa</q17:TrackingLocationAddress2Romanized>' 
+'<q17:TrackingLocationZIPRomanized>1220</q17:TrackingLocationZIPRomanized>' 
+'<q17:TrackingLocationCityRomanized>SOFIA</q17:TrackingLocationCityRomanized>' 
+'<q17:TrackingLocationCountry>BG</q17:TrackingLocationCountry>' 
+'<q17:TrackingLocationGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'<q17:TrackingLocationCoord xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' 
+'</q17:TrackingLocationSection>' 
+'</q1:DocumentSection>' 
+'</tns:StoreDocumentMessage>' 
+'</tns:StoreDocument>' 
+'</soapenv:Body>' 
+'</soapenv:Envelope>' 

DECLARE @name varchar(250); 
SET @name='StoreDocument'+'.xml' 

EXEC ExportXML @XMLEXPOPRT,'D:\TT',@name; 
END 

GO 
+0

要使用/保留使用「FOR XML」子句生成的元素的名稱空間,請使用「FOR XML RAW」(請參閱​​https://docs.microsoft.com/zh-cn/sql/relational-數據庫/ xml/add-namespaces-to-queries-with-with-xmlnamespaces)帶或不帶WITH XMLNAMESPACES('uri'as ns1)子句... –