Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Save SP Query Result to TXT file

Author  Topic 

peterstone
Starting Member

1 Post

Posted - 2009-06-17 : 11:07:59
Hi,

I am using SQL 2005.

I have a SP which creates temp tables from a selection of other tables.
At the end of the SP, there is a select statement on a temp table.

I want to save the results of select statement to a TXT file. The catch is this - the TXT file name changes everyday. The name of the TXT file will be the day that it ran on (eg: if it runs on the 01 March 2009 the name of the file must be 01.txt and if it runs on the 02 March 2009 the name of the file must be 02.txt etc..).

I know how to save a file but not with a changing name.

I would like this all done in SQL.
Can somebody assist me with this?

Thanks
Kyle

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-17 : 11:12:23
Easiest is to save the data to a global temp table then bcp that out to a text file.

Do you know how to do that?
What is the method you were thinking of?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-17 : 11:12:31
you can use bcp with query out option. and give query as exec procedure.... . Pass the file name as dynamic and execute it using exec. see below for example

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/bcp-export-data-to-text-file.aspx
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-17 : 11:15:12
quote:
Originally posted by visakh16

you can use bcp with query out option. and give query as exec procedure.... . Pass the file name as dynamic and execute it using exec. see below for example

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/bcp-export-data-to-text-file.aspx



Probably not if the SP uses temp tables - depends on the version though.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -