Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a table in SQL Server 2008

  • How do I Export a table SQL Server to a single flat file? (all data and the first row as column names)??
  • Is there a query that do this?
like image 490
edgarmtze Avatar asked Feb 23 '11 01:02

edgarmtze


People also ask

Can we export the table from SQL Server?

Right-click on the database name, and select Tasks - Export Data ... 4. From the Data Source drop-down list, select SQL Server native Client 10.0 ; ensure that the correct SQL server name and database are selected, and the Authentication is set to Windows Authentication. Click Next.

How do I export a table from SQL Server to Excel?

Go to "Object Explorer", find the server database you want to export to Excel. Right-click on it and choose "Tasks" > "Export Data" to export table data in SQL.


2 Answers

  1. Right Click over the Database name -> Tasks -> ExportData
  2. Choose the table as Data Source
  3. Choose Flat file destination as destination
  4. Choose a FileName
  5. Mark "Column Names in the first data row"
like image 104
pcofre Avatar answered Sep 17 '22 12:09

pcofre


DECLARE  
     @saveas VARCHAR(2048)
    ,@query VARCHAR(2048)
    ,@bcpquery VARCHAR(2048)
    ,@bcpconn VARCHAR(64)
    ,@bcpdelim VARCHAR(2)

SET @query      = 'select * from 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  
like image 34
Malk Avatar answered Sep 18 '22 12:09

Malk