I am using SQL Server 2005 SP2. I have a need to add / append text (one line
each time) to an o/p text file in c: (say c:\test\logdate.txt) from within a
stored proc. This adding happens frequently like a log file.
Is there is any way for doing this? We do not have permission to use
xp_cmdshell.
--
Regards
WT
It can be questioned whether it's a good idea to write to a file from a
stored procedure if it's going to happen often. It does not sound like a
good recipe for scalability.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
create procedure file_write_text(@file_name varchar(1000),
@text varchar(8000),
@append bit = 0)
as
/*
exec file_write_text 'c:\foo.txt','hello'
exec file_write_text 'c:\foo.txt',' world',1
*/
declare @fso int
declare @ts int
declare @rv int
exec @rv = sp_oacreate "scripting.filesystemobject", @fso OUTPUT, 1
if @rv <> 0 goto eh
if @append = 1
begin
--open the text stream for append, will fail if the file doesn't exist
exec @rv = sp_oamethod @fso,"opentextfile", @ts OUTPUT, @file_name, 8
if @rv <> 0 goto eh
end
else
begin
--create a new text file, overwriing if necessary
exec @rv = sp_oamethod @fso,"createtextfile", @ts OUTPUT, @file_name, -1
if @rv <> 0 goto eh
end
exec @rv = sp_oamethod @ts,"write",null ,@text
if @rv <> 0 goto eh
exec @rv = sp_oamethod @ts,"close"
if @rv <> 0 goto eh
exec sp_oadestroy @ts
exec sp_oadestroy @fso
return 0
eh:
declare @es varchar(512)
declare @ed varchar(512)
exec sp_oageterrorinfo null, @es OUTPUT, @ed OUTPUT
raiserror(@ed,16,1)
exec sp_oadestroy @ts
exec sp_oadestroy @fso
return 1
"Princy" <non...@discussion.mc.com> wrote in message
news:1794BCA2-FB7F-43A9...@microsoft.com...
insert into openrowset
(
'Microsoft.Jet.OLEDB.4.0',
'Text;Database=C:\TEMP\;HDR=Yes;',
'select A, B from sysobjects.csv'
)
select object_id, schema_id
from sys.objects
--
Regards
WT
Did you consider to implement this in the CLR? Or use SSIS which I think
was suggested in another post?
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx