为查询返回的每条记录生成单独的XML文件

时间:2017-10-27 12:31:47

标签: sql-server xml

我有以下查询,一个更大的查询的一部分,用于生成XML:

DECLARE @XMLProductsLines NVARCHAR(MAX)
SET @XMLProductsLines=''

;WITH XMLNAMESPACES ('http://ITrack.Transmission/2011/02/25/Objects' as q17)

SELECT ProductCode 'q17:ProductCode', OrderedQuantity 'q17:OrderedQuantity'
FROM PurchasesDocumentsLines 
FOR XML RAW ('q17:OrderedProductSection'), ROOT('q17:OrderedProducts'), ELEMENTS

查询的输出如下:

<q17:OrderedProducts xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">
  <q17:OrderedProductSection>
    <q17:ProductCode>2139265</q17:ProductCode>
    <q17:OrderedQuantity>5000</q17:OrderedQuantity>
  </q17:OrderedProductSection>
  <q17:OrderedProductSection>
    <q17:ProductCode>2131602</q17:ProductCode>
    <q17:OrderedQuantity>6000</q17:OrderedQuantity>
  </q17:OrderedProductSection>
  <q17:OrderedProductSection>
    <q17:ProductCode>2131601</q17:ProductCode>
    <q17:OrderedQuantity>700</q17:OrderedQuantity>
  </q17:OrderedProductSection>
  <q17:OrderedProductSection>
    <q17:ProductCode>2131416</q17:ProductCode>
    <q17:OrderedQuantity>5000</q17:OrderedQuantity>
  </q17:OrderedProductSection>
</q17:OrderedProducts>

有没有办法去&#34;拆分&#34;将结果分成多个XML文件,使每个<ProductCode><OrderedQuantity>具有不同的XML文件?在这种情况下,我应该有4个文件。感谢

DDL

CREATE TABLE PurchasesDocumentsLines
(
ProductCode NVARCHAR (4000),
OrderedQuantity DECIMAL (20, 0)
);

INSERT INTO PurchasesDocumentsLines (ProductCode, OrderedQuantity)
VALUES('2139265', '5000'), ('2131602', '6000'), ('2131601', '700'), ('2131416', '5000');

WITH XMLNAMESPACES('http://ITrack.Transmission/2011/02/25/Objects' AS q17)
SELECT Code AS [q17:DestinationCode]
      ,Description AS [q17:DestinationName]
      ,Address1 AS [q17:DestinationAddress1]
      ,Address2 AS [q17:DestinationAddress2]
FROM PurchasesDocumentsLines
FOR XML PATH('q17:OrderedProducts'),ELEMENTS XSINIL,ROOT('q17:OrderedProductSection');

修改

我已经声明并使用了CURSOR,但它只返回1个值(列表中的第一个)。我错过了什么?

DECLARE @Number NVARCHAR(MAX)=''
DECLARE @XMLSalesOrders NVARCHAR(MAX)=''
DECLARE @Root NVARCHAR(MAX)=''

DECLARE Records CURSOR FAST_FORWARD

FOR SELECT DISTINCT ProductCode FROM PurchasesDocumentsLines

OPEN Records
FETCH NEXT FROM Records INTO @Number
WHILE @@FETCH_STATUS = 0
BEGIN 
SET @XMLSalesOrders=''

;WITH XMLNAMESPACES ('http://ITrack.Transmission/2011/02/25/Objects' as q17)

SELECT @XMLSalesOrders=( SELECT ProductCode 'q17:ProductCode', OrderedQuantity 'q17:OrderedQuantity'
FROM PurchasesDocumentsLines 
FOR XML RAW ('q17:OrderedProductSection'), ROOT('q17:OrderedProducts'), ELEMENTS)


FETCH NEXT FROM Records INTO @Number
END
CLOSE Records
DEALLOCATE Records

--SET @Root='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'+@XMLSalesOrders+'</soapenv:Envelope>'

PRINT @Number

修改2

除了生成XML之外,我还使用以下查询和从网站上获取的存储过程来保存本地笔记本电脑上的文件。但是,在SQL中生成XML后,我的文件夹只保存了一个文件。有没有其他方法可以将XML文件保存在我的笔记本电脑上,而不是我正在使用的那个?

调用程序的查询

DECLARE @Root NVARCHAR(MAX)=''
SET @Root='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'+@XMLSalesOrders+'</soapenv:Envelope>'

DECLARE @XMLFileDescription NVARCHAR(250);
SET @XMLFileDescription='StoreDocument'+FORMAT(GETDATE(), 'yyyMMddHHmmssmmm')+'.xml'

EXEC ExportXML @Root,'D:\TT', @XMLFileDescription

程序

CREATE PROCEDURE [dbo].[ExportXML] 
  (
    @String Varchar(max), --8000 in SQL Server 2000
    @Path VARCHAR(255),
    @Filename VARCHAR(100)
  )

AS
DECLARE  @objFileSystem int,
         @objTextStream int,
         @objErrorObject int,
         @strErrorMessage Varchar(1000),
         @Command varchar(1000),
         @hr int,
         @fileAndPath varchar(80)

set nocount on

select @strErrorMessage='opening the File System Object'
EXECUTE @hr = sp_OACreate  'Scripting.FileSystemObject' , @objFileSystem OUT

Select @FileAndPath=@path+'\'+@filename
    if @HR=0 Select @objErrorObject=@objFileSystem , @strErrorMessage='Creating file "'+@FileAndPath+'"'
    if @HR=0 execute @hr = sp_OAMethod   @objFileSystem   , 'CreateTextFile' , @objTextStream OUT, @FileAndPath,2,True

    if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='writing to the file "'+@FileAndPath+'"'
    if @HR=0 execute @hr = sp_OAMethod  @objTextStream, 'Write', Null, @String

    if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='closing the file "'+@FileAndPath+'"'
    if @HR=0 execute @hr = sp_OAMethod  @objTextStream, 'Close'

    if @hr<>0
    begin
    Declare 
        @Source varchar(255),
        @Description Varchar(255),
        @Helpfile Varchar(255),
        @HelpID int

        EXECUTE sp_OAGetErrorInfo  @objErrorObject, @source output,@Description output,@Helpfile output,@HelpID output
        Select @strErrorMessage='Error whilst ' +coalesce(@strErrorMessage,'doing something') +', '+coalesce(@Description,'')
        raiserror (@strErrorMessage,16,1)
    end
EXECUTE  sp_OADestroy @objTextStream
EXECUTE sp_OADestroy @objTextStream
GO

1 个答案:

答案 0 :(得分:0)

DECLARE @ProductCode AS NVARCHAR(4000)
DECLARE @Quantity AS DECIMAL(20, 0) 

DECLARE Records CURSOR FAST_FORWARD FOR
SELECT  ProductCode 
      , OrderedQuantity 
FROM dbo.PurchasesDocumentsLines

OPEN Records
FETCH NEXT FROM Records INTO @ProductCode, @Quantity
WHILE @@FETCH_STATUS = 0
BEGIN 

WITH XMLNAMESPACES('http://ITrack.Transmission/2011/02/25/Objects' AS q17)
SELECT @ProductCode AS [q17:DestinationCode]
      ,@Quantity AS [q17:DestinationName]
FOR XML PATH('q17:OrderedProducts'),ELEMENTS XSINIL,ROOT('q17:OrderedProductSection');

FETCH NEXT FROM Records INTO @ProductCode, @Quantity
END

CLOSE Records
DEALLOCATE Records