dbTalk Databases Forums  

generate .txt file from a stored procedure

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss generate .txt file from a stored procedure in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sandra
 
Posts: n/a

Default generate .txt file from a stored procedure - 09-30-2003 , 05:31 PM






How do I generate a .txt file from a Stored Procedure.
Something like this:

select * from my_table
into c:\temp\my_text_file.txt

Thanks in advance for any help.

Sandra

Reply With Quote
  #2  
Old   
Sue Hoegemeier
 
Posts: n/a

Default Re: generate .txt file from a stored procedure - 09-30-2003 , 07:20 PM






There isn't anything to directly do this (e.g.nothing like
an option for stored procedures results to text) but there
are several ways to accomplish this.
Using the command line utilities, you could call xp_cmdshell
and use osql to output the query results to a file. Along
the same lines with xp_cmdshell, you could use bcp out.
You could write an extended stored procedure to do this -
and a variety of methods in the xp to do this (ado and file
system object, etc.)
DTS is probably the easiest method though - just use a text
file destination and a data pump to drop the query results
to a text file. And you could call the package from a stored
procedure using dtsrun (or sp_OA procedures).
You could have a combination of ADO and FileSystemObject and
call this in other ways than just an extended stored
procedure - script file, external app, etc.
And as I type I keep thinking of a lot of ways to do it
along these lines. It really depends more on where you are
calling the stored procedure from.

-Sue

On Tue, 30 Sep 2003 15:31:58 -0700, "Sandra"
<silvah (AT) wellsfargo (DOT) com> wrote:

Quote:
How do I generate a .txt file from a Stored Procedure.
Something like this:

select * from my_table
into c:\temp\my_text_file.txt

Thanks in advance for any help.

Sandra


Reply With Quote
  #3  
Old   
Frank Matthiesen
 
Posts: n/a

Default Re: generate .txt file from a stored procedure - 10-01-2003 , 01:14 PM



In News-Entry:0d4001c387a2$a92ce7d0$a101280a (AT) phx (DOT) gbl,
Sandra <silvah (AT) wellsfargo (DOT) com> wrote:
Quote:
How do I generate a .txt file from a Stored Procedure.
Something like this:

select * from my_table
into c:\temp\my_text_file.txt
Use BCP.
The follwing line put the content of tbl_test into tbl_test.txt (in
DOS-Console)

bcp "MyDBName.MyDBUsername.tbl_test" out
"c:\tbl_test.txt" -c -q -U"MYDBUsername" -P"MyDBPassword"
If you want to use this string in a sproc you have to quote the command with
' and start it with xp_cmdshell

master.dbo.xp_cmdshell 'bcp "MyDBName.MyDBUsername.tbl_test" out
"c:\tbl_test.txt" -c -q -U"MYDBUsername" -"MyDBPassword"'


usage: BCP {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-6 6x file format] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"]


kind regards

Frank
www.xax.de





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.