September 5, 2006 at 5:46 am
Hi there!
Is there an easy way to direct SQL query output to a text file? ... For instance, to run SELECT * FROM authors and send the results to a text file, ...
I need this in T-sql only. Not in VB script
Thanks
September 5, 2006 at 5:53 am
Yep... use OSQL to run the query from a CMD prompt (or using xp_CmdShell w/ trusted connection) with a redirect (the -o option) to a text file.
See "Books Online" for the full syntax... if you run into problems with the syntax, post back and we'll help...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 5, 2006 at 6:57 am
Hi jeff
i tried below but can't get round it. Please help.
DECLARE @cmd VARCHAR(5000)
SET @cmd = 'OSQL -S support-ts2 -dNCL_REMOTE_SP6D '
+ ' -Usa -Pseaward'
+ ' -Q"select notes from person where len(notes) > 1000"'
+ ' -oc:\authors.txt'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT
c:\authors.txt is the file i wanna write to.
Thanks a lot mate
September 5, 2006 at 7:15 am
One thing you can do is this :
DECLARE @cmd VARCHAR(5000)
SET @cmd = 'OSQL -S support-ts2 -dNCL_REMOTE_SP6D '
+ ' -Usa -Pseaward'
+ ' -Q"select notes from person where len(notes) > 1000"'
+ ' -oc:\authors.txt'
PRINT @cmd
--my results
OSQL -S support-ts2 -dNCL_REMOTE_SP6D -Usa -Pseaward -Q"select notes from person where len(notes) > 1000" -oc:\authors.txt
then manually run that in OSQL and make corrections untill you get it working. Then come back and fix the string.
September 5, 2006 at 7:44 am
Vijay,
You can debug the query by checking the authors.txt file.. It contains the vital error information
--Ramesh
--Ramesh
September 5, 2006 at 7:52 am
thanks guys..
i managed to do it...
DECLARE @cmd VARCHAR(5000)
SET @cmd = 'OSQL -S support_svr1 -d NCLREMOTE_SP6D '
+ ' -U sa -P seaward'
+ ' -Q "select notes from person where len(notes) > 1000"'
+ ' -o c:\Notes.txt'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT
September 5, 2006 at 8:05 am
BTW you might want to change your login credentials now .
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply