Link to home
Start Free TrialLog in
Avatar of alenknight
alenknight

asked on

export results to text file sql server 2005

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
Avatar of sbagireddi
sbagireddi

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'
Avatar of alenknight

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of sbagireddi
sbagireddi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial