SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to write query result in an output file in T-SQL (MSDE)


How to write query result in an output file in T-SQL (MSDE)

Author
Message
littlesquall
littlesquall
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 42
I'm looking everywhere to write easily the result of an T-SQL query in an output file.

I work with MSDE (SQL2000) an the T-SQL code is in a stored procedure.

The query is like :
"select * from myTable where ..."
There are about 150 fields in the table to return in the output file.

I would like to write the line result in one line in an output file in the hard drive.

I've tryed 2 method :

1) Use of "xp_cmdshell" command
DECLARE @PrintInLogFile sysname
DECLARE @var sysname
SET @var = ...
SET @PrintInLogFile = 'echo ' + @var + ' > C:\File.log'
EXEC xp_cmdshell @PrintInLogFile

The problem is that I can't use this command in production because of security constraints.

2) Use of "sp_OAMethod" command with 'WriteLine' method

--variables d'écriture
DECLARE @FS int
DECLARE @FileID int
DECLARE @iCOMret int

DECLARE @FileName VARCHAR(4000)

SET @FileName='C:\File.log'

--creation de l objet pour manipulation du fichier de log
EXECUTE @iCOMret = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
EXECUTE @iCOMret = sp_OAMethod @FS, 'CreateTextFile', @FileName, true

--Ouvre le fichier (2 = ForWriting, 8 = ForAppending)
EXECUTE @iCOMret = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 2

EXECUTE @iCOMret = sp_OAMethod @FileID, 'WriteLine', Null, 'Bonjour'

-- Fermeture fichier de log
EXECUTE @iCOMret = sp_OADestroy @FileID
EXECUTE @iCOMret = sp_OADestroy @FS

The problem with this method is that I can't write one line with the resul of a select *


Can anyone help me ?
I'm sure that in T-SQL we can write easily the entirely result of a select * in one line of an output file in the hard drive...

Thnaks in advance !
SQL_ABD
SQL_ABD
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1542 Visits: 1349
Hi,
Check this link http://www.sqlteam.com/article/osql-storing-result-of-a-stored-procedure-in-a-file

Regards,
Ahmed
littlesquall
littlesquall
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 42
Thanks Ahmed

I've already tried this method

osql -E -d NOMADE -S (local) -Q "select * from MyTable where ..." -o C:\File.txt

But I can't launch the command from a cmd window. THe T-SQL code must be in a stored procedure.
In addition, the return result is written on multiple lines in the output file.
I would like to write the return result of a select * command into one output line.

Thank you for helping me
SQL_ABD
SQL_ABD
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1542 Visits: 1349
Hi,

You can use xp_cmdshell to execute from a stored proc
Exec master..xp_cmdshell 'osql -E -d NOMADE -S (local) -Q ''select * from MyTable where ...'' -o C:\File.txt'
littlesquall
littlesquall
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 42
As I've explain in my first post, I can't use this method for security reason.
Maybee an other one ?
Carl Federl
Carl Federl
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3376 Visits: 4350
Writing to a file from SQL is not supported, so you will need to use one of the SQL Server add-on services.

How is the process to write the file being invoked ?
If on a scheduled basis, a SQL Server job can run a Data Transformation Services package.
If based on some data changes, Notification Services can be used.
Alternatively, Reporting Services could be used.

SQL = Scarcely Qualifies as a Language
SQL_ABD
SQL_ABD
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1542 Visits: 1349
Hi Carl,
I already redirect the result to file using osql (whithin a stored proc and xp_cmdshell ) or using sp_OACreate 'Scripting.FileSystemObject' and it works fine.
The problem is littlesquall can not use the 2 methods because of security constraints (The network admin does not allow)
Regards,
Ahmed
littlesquall
littlesquall
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 42
Thanks Ahmed,

The 1st method with xp_cmdshell cannot be used because of security method.

With the second one (with execute @OLEResult = sp_OAMethod @FileID ,'WriteLine', Null, @Str)
I don't arrive to return the entirely result of the select * in one line in an output file.

Maybee there is an other method than WriteLine or Write to return easily the result of a select *...
SQL_ABD
SQL_ABD
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1542 Visits: 1349
Hi littlesquall,
You want one line in the output. It means you want each 150 fields in one line, or something else.
I think the line length of the text file is limited, then when you reach a certain value, he will go to the next line.
May be you need to do some process on the fields, before generating yor output text file.
Regards,
Ahmed
littlesquall
littlesquall
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 42
Of course ahmed,

With the editor used, it can appears on multiple lines. It's not really a problem...

But I can't use like "OSQL ... -o C:\Output.txt" does.
The result of a select * in the output file is totally random,
like it can appear in a cmd windows when the select * returns more than 150 fields...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search