Sql server 为查询返回的每个记录生成单独的XML文件
我有以下查询,是更大查询的一部分,用于生成XML:Sql server 为查询返回的每个记录生成单独的XML文件,sql-server,xml,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
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>
编辑
我声明并使用了如下游标,但它只返回列表中第一个值的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文件吗
调用过程的查询
程序
我不喜欢游标,但在这种情况下,游标是合理的。@KamilNowinski-实际上这就是我现在正在尝试的。为什么每个人都在逃避诅咒?我真的不太了解它们,但我看到很多人认为游标是一种不好的做法,因为在使用数据库时,你应该使用数据集。只有这样你才能达到正确的效率。光标只在特定情况下有用。@KamilNowinski-你能看一下吗?是我错过的东西,显然我不知道是什么。谢谢。它可以工作,但我需要额外使用它来保存笔记本电脑上的文件。请看我的编辑。
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
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
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