Solved

export results to text file sql server 2005

Posted on 2008-06-20
3
3,978 Views
Last Modified: 2012-06-21
could someone walk me through the process of getting this script out to a text file in sql server 2005?
i thought i could just do SQLCMD mode ... but that doens't work.  its just the adventure works db ....

here's the syntax i have... feel ffree to edit it as you wish.  again... i would like instructions on what to do... where to go... what to click... etc.

bcp "SELECT top 10 * from dbo.databaselog" queryout c:\test.txt
0
Comment
Question by:alenknight
  • 2
3 Comments
 
LVL 8

Expert Comment

by:sbagireddi
ID: 21835097
This is a more generic script but reusable:


CREATE Procedure BCP_Text_File
(
@table varchar(100),
@FileName varchar(100)
)
as
If exists(Select top 10* from information_Schema.tables where table_name='databaselog')
    Begin
        Declare @str varchar(1000)
        set @str='Exec Master..xp_Cmdshell ''bcp "Select * from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c'''
        Exec(@str)
    end
else
    Select 'The table '+@table+' does not exist in the database'

EXEC BCP_Text_File 'DatabaseLog','C:\DatabaseLog.txt'
0
 

Author Comment

by:alenknight
ID: 21835112
isn't bcp supposed to work by itself?  why create a stored procedure?  i would like to not have to edit a stored procedure each time i wanna change table
0
 
LVL 8

Accepted Solution

by:
sbagireddi earned 500 total points
ID: 21835117
Oops..typo


Corrected script:


CREATE Procedure BCP_Text_File
(
@table varchar(100),
@FileName varchar(100)
)
as
If exists(Select * from information_Schema.tables where table_name='databaselog')
    Begin
        Declare @str varchar(1000)
        set @str='Exec Master..xp_Cmdshell ''bcp "Select top 10* from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c'''
        Exec(@str)
    end
else
    Select 'The table '+@table+' does not exist in the database'

EXEC BCP_Text_File 'DatabaseLog','C:\DatabaseLog.txt'
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now