1

我需要从我所做的单个查询中创建多个 xml 文件(每行)。我的查询正在生成 10,000 个 xml 文件,因为您知道 SQL Server 为每个 xml 文件分配不同的名称,现在我需要将它们自动保存在 c:\ 中。我可以看到所有文件,但我无法保存它们,任何建议或帮助将不胜感激。另一个提示:从 SQL 服务器,我们可以从 SQL 服务器文件选项卡中一一保存每个 xml。

    CREATE TABLE dbo.sample(
    [BTno] [nvarchar](25) NULL,
    [First_Name] [nvarchar](35) NULL,
    [Last_Name] [nvarchar](35) NULL,
    [BTid] [nvarchar](15) NULL,
) ON [PRIMARY];

insert into dbo.sample values('1B','Vartan','Sarkis','69876');
insert into dbo.sample values('2B','Anoush','Eric','87656');
insert into dbo.sample values('3B','Lucine','Arpiar','65467');
insert into dbo.sample values('4B','Anum','Noor','98076');
insert into dbo.sample values('5B','Abercio','Banninq','34897');
insert into dbo.sample values('1C','Gaea','Nishan','29841');
insert into dbo.sample values('7B','Marilyn','Vahe','78903');
insert into dbo.sample values('2Z','Bansi','Aakarshan','34905');
insert into dbo.sample values('9S','Eric','Abban','45892');
insert into dbo.sample values('12B','Dave','Tate','19994');

-- 这里是生成多个xml文件的查询

select ((select * from dbo.sample a where a.BTid = b.BTid
         for xml path('Row'),TYPE, ROOT('BT')
        )) from dbo.sample b
4

2 回答 2

0

试试这个脚本,也许会有用:

CREATE PROCEDURE saveFile (@file varchar(1000), @txt VARCHAR(8000)) AS  
BEGIN  
    DECLARE @sys_obj INT  
    DECLARE @rst INT  
    DECLARE @fID INT  

    EXECUTE @rst = sp_OACreate 'Scripting.FileSystemObject', @sys_obj OUT  
    EXECUTE sp_OAMethod @sys_obj, 'FileExists', @rst OUT, @file   
    IF @rst=0   
        BEGIN     
            EXECUTE sp_OAMethod @sys_obj, 'OpenTextFile', @fID OUT,  @file, 8, 1  
            EXECUTE sp_OAMethod @fID, 'WriteLine', Null, @txt  
            EXECUTE sp_OADestroy @fID  
            EXECUTE sp_OADestroy @sys_obj  
        END
END

通过这一行,可以将字符串(例如 XML)保存到磁盘:

EXEC saveFile @file = 'C:\test.xml', @txt = '<xml><test>123</test></xml>'

这里是保存所有返回查询的字段的其他查询:

DECLARE cur CURSOR LOCAL FAST_FORWARD FOR
            select CAST(((select * from dbo.sample a where a.BTid = b.BTid
            for xml path('Row'),TYPE, ROOT('BT')
            )) AS VARCHAR(MAX)) AS TXT from dbo.sample b
    --HERE GO YOUR QUERY
    /*Example
            SELECT  '<xml><test>111</test></xml>' AS TXT
    UNION
    SELECT  '<xml><test>222</test></xml>' AS TXT
    UNION
    SELECT  '<xml><test>333</test></xml>' AS TXT
    */
DECLARE @v_count AS INTEGER
DECLARE @v_txt AS VARCHAR(8000)
DECLARE @v_filename AS VARCHAR(100)

SET @v_count = 1
SET @v_txt = ''

OPEN cur FETCH NEXT FROM cur INTO @v_txt

WHILE @@FETCH_STATUS = 0 
    BEGIN
        SET @v_filename = 'C:\test' + CAST(@v_count AS VARCHAR) + '.xml'
        EXEC saveFile @file = @v_filename, @txt = @v_txt
        SET @v_count = @v_count + 1
        FETCH NEXT FROM cur INTO @v_txt
    END
CLOSE cur
DEALLOCATE cur
于 2013-07-19T14:52:52.923 回答
0

我作弊并使用其他人的功能进行实际写入。请注意,您需要启用“Ole 自动化程序”才能使写入程序正常工作。一些公司的安全策略对启用这些东西有点敏感。基本上我假设如果未启用此功能,您可以这样做。write 函数还将覆盖具有相同文件名的文件,尽管这可能与您的需要无关。

declare @thexml xml
declare @xmlstring varchar(max)
declare @path varchar(255)
declare @filename_start varchar(100)
declare @filename varchar(100)
declare @count int

set @path='C:\testdata\'
set @filename_start = 'xmlname'
set @count = 0

declare xcursor cursor for
select ((select * from dbo.sample a where a.BTid = b.BTid
         for xml path('Row'),TYPE, ROOT('BT')
        )) from dbo.sample b

open xcursor

fetch xcursor into @thexml

while @@fetch_status <> -1 begin
  set @count = @count + 1
  set @filename = @filename_start + cast(@count as varchar) + '.xml'
  set @xmlstring = cast(@thexml as varchar(max))
  exec master.dbo.spWriteStringToFile @xmlstring,@path,@filename

  fetch xcursor into @thexml
end

deallocate xcursor

启用 OLE - http://msdn.microsoft.com/en-us/library/ms191188.aspx

写程序的功劳 - https://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/

实际写入过程(必须从更改更改为创建) - https://www.simple-talk.com/code/WorkingWithFiles/spWriteStringTofile.txt

于 2013-07-19T15:25:03.343 回答