使用 SQL Server 2008 在本地计算机上创建文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8132663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:15:18  来源:igfitidea点击:

Creating a text file on local machine using SQL Server 2008

sqlsql-server-2008tsqltextexport-to-csv

提问by masif

I would like to know how to create a text file and save it on my local machine, below is the stored procedure I am using. It creates the text file on server but I want it to be created on my local machine.

我想知道如何创建一个文本文件并将其保存在我的本地机器上,下面是我正在使用的存储过程。它在服务器上创建文本文件,但我希望它在我的本地机器上创建。

Any help is much appreciated..

任何帮助深表感谢..

alter Procedure [dbo].[USP_SaveFile]
    (@text as NVarchar(Max), @Filename Varchar(200)) AS
Begin
    declare
        @Object int,
        @rc int,
        @FileID Int

    EXEC @rc = sp_OACreate 'Scripting.FileSystemObject', @Object OUT
    EXEC @rc = sp_OAMethod @Object , 'OpenTextFile' , @FileID OUT , @Filename , 2 , 1
    Set @text = Replace(Replace(Replace(@text,'&','&'),'<' ,'<'),'>','>')
    EXEC @rc = sp_OAMethod @FileID , 'WriteLine' , Null , @text
    Exec @rc = [databasename].dbo.sp_OADestroy @FileID

    Declare @Append bit

    Select @Append = 0

    If @rc <> 0
    Begin
        Exec @rc = [databasename].dbo.sp_OAMethod @Object, 'SaveFile',null,@text ,@Filename,@Append
    End

    Exec @rc = [databasename].dbo.sp_OADestroy @Object
End

--  Use [databasename]
--  EXEC sp_configure 'show advanced options', 1
--  GO
--  EXEC sp_configure 'Ole Automation Procedures', 1;
--  GO
--  RECONFIGURE;
--  GO
--  EXEC USP_SaveFile 'Microsoft SQL Server 2008', 'D:\sqlfiles\test.txt'

回答by Micha? Powaga

Create a share on your local machine and then use it like this:

在本地机器上创建一个共享,然后像这样使用它:

EXEC USP_SaveFile 'Microsoft SQL Server 2008', '\YourMachine\dir1\dir2\test.txt'

Please mind that SQL Server is a service so this: Network Service account accessing a folder shareapply to it.

请注意 SQL Server 是一项服务,因此:访问文件夹共享的网络服务帐户适用于它。