1

I have a temp table in sql server 2008 and I need to create a csv of txt file by tsql from that temp table.

What is the correct way for it?

Thanks.

  • No it's not, i need to create file by code, The link you send tells how to create by manangment studio tools. thanks anyway. –  Oct 04 '16 at 06:20
  • I removed my vote. – Tim Biegeleisen Oct 04 '16 at 06:21
  • 1
    Look into this: http://stackoverflow.com/questions/425379/how-to-export-data-as-csv-format-from-sql-server-using-sqlcmd – Tim Biegeleisen Oct 04 '16 at 06:21
  • Actualyy I need to make it using sql query:) –  Oct 04 '16 at 06:39
  • AFAIK you need to use a tool for this, q.v. [here](http://dba.stackexchange.com/questions/23566/writing-select-result-to-a-csv-file), you can't just write a basic query from the studio. – Tim Biegeleisen Oct 04 '16 at 06:40
  • 2
    You can write a select query including all the fields you need and Go to Tools > Options > Query Results > SQL Server > Results To Text. When you run the query it will prompt you to save the file. I guess it will let you save the file in rpt or txt format. You can open it via excel or a text editor. There are few ways of exporting this is one of them. Not recommending this for huge datasets. Hope this helps. – sheeni Oct 04 '16 at 07:22

2 Answers2

0
select col1 + "," + col2 
from table

send results to file

paparazzo
  • 42,665
  • 20
  • 93
  • 158
0

If you provide tempdb.. it can access temp tables

exec master..xp_cmdshell 'bcp " select * from tempdb..#tables" queryout c:\test.txt -c -T'
Kannan Kandasamy
  • 11,701
  • 3
  • 19
  • 31
  • When I try to --> ** 'bcp "select filecontent from tempdb..#tempbanktable" queryout "D:\test.csv" -c ** I am getting this error --> ** Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Code page 857 is not supported by SQL Server ** –  Oct 04 '16 at 10:30
  • ]which version of sql server you are using? – Kannan Kandasamy Oct 04 '16 at 10:45
  • looks like codepage issue.. Try using '-w' parameter – Kannan Kandasamy Oct 04 '16 at 10:46