开发者

How do you export to a file in a Stored Proc?

开发者 https://www.devze.com 2022-12-11 21:47 出处:网络
I tried BCP, but doesn\'t work.Sql keeps giving me some Help开发者_如何学编程 text.. Anyway, it doesn\'t matter, as I don\'t want to paste the contents of the stored proc AGAIN into a string so this

I tried BCP, but doesn't work. Sql keeps giving me some Help开发者_如何学编程 text..

Anyway, it doesn't matter, as I don't want to paste the contents of the stored proc AGAIN into a string so this can export.

My Question: I have an existing stored proc. I would like it to automatically kick its result out into a text file.

Any clues?


this is very easy

CREATE PROCEDURE sp_AppendToFile(@FileName varchar(255), @Text1 varchar(255)) AS DECLARE @FS int, @OLEResult int, @FileID int

EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject' :Open a file execute @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 1 IF @OLEResult <> 0 PRINT 'OpenTextFile'

--Write Text1 execute @OLEResult = sp_OAMethod @FileID, 'WriteLine', Null, @Text1 IF @OLEResult <> 0 PRINT 'WriteLine'

EXECUTE @OLEResult = sp_OADestroy @FileID EXECUTE @OLEResult = sp_OADestroy @FS

from: http://www.motobit.com/tips/detpg_SQLWrFile/


Assuming you have stored procedure MyProc in database MyDB on server MyServer, and you want results in tab delimited output file MyResults:

bcp "exec MyDB.dbo.MyProc" queryout MyResults -T -c -t\t -S MyServer

should work. bcp offers a lot of other options for output as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消