need help to export table from sql server 2008 to text file -


i trying export table present in ms sql server 2008 text file on system. writing following command in sql server query window

select * [adventureworks].[person].[addresstype]  outfile 'c:/filename.csv' fields terminated ',' lines terminated '\n'; 

now whenever write command sql gives me error incorrect syntax near 'into'

then tried interchanging , keywords follows

select * outfile 'c:/filename.csv' fields terminated ',' lines terminated '\n' [adventureworks].[person].[addresstype] ; 

now gives me error incorrect syntax near 'c:/filename.csv'

please me regarding this. not able remove these error , working sql

there more many ways solve problem , in case here 2 solutions

solution 1

  • right click on database name -> tasks -> export data
  • choose table data source
  • choose flat file destination destination
  • choose file-name ( file name )
  • mark "column names in first data row" ( opitional)

and that's it.

solution 2

declare    @saveas varchar(2048) ,@query varchar(2048) ,@bcpquery varchar(2048) ,@bcpconn varchar(64) ,@bcpdelim varchar(2)   set @query      = 'select * table1'  set @saveas     = '\\server1\share1\folder\queryoutput.txt'  set @bcpdelim   = '|'  set @bcpconn    = '-t' -- trusted  --set @bcpconn    = '-u <username> -p <password>' -- sql authentication    set @bcpquery = 'bcp "' + replace(@query, char(10), '') + '" queryout "' + @saveas + '" -c -t^' + @bcpdelim + ' ' + @bcpconn + ' -s ' + @@servername exec master..xp_cmdshell @bcpquery   

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -