exec Spwritestringtofile 'select * from sysobjects'
,'c:\temp\', 'result.csv'
,@colheaders=0
,@lastupdated=0
DECLARE @FILE VARCHAR(8000)
DECLARE @ROW VARCHAR(1000)
DECLARE crs_file CURSOR FOR
SELECT line
FROM dados
OPEN crs_file
SET @FILE = ''
SET @ROW = ''
FETCH crs_file
INTO @ROW
WHILE @@FETCH_STATUS = 0
BEGIN
IF Charindex('Standard',@ROW) > 0
SET @FILE = @FILE + @ROW
ELSE
SET @FILE = @FILE + @ROW + Char(13) + Char(10)
FETCH NEXT FROM crs_file
INTO @ROW
END
EXEC Spwritestringtofile
@FILE ,
'C:\TEMP' ,
'OUT.TXT'
CLOSE crs_file
DEALLOCATE crs_file
alter PROCEDURE Spwritestringtofile(
@String VARCHAR(8000), --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