Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL BACKUP Query

how can i backup data from a query with MSSQL. I think about something like this:

BACKUP DATABASE 'sourceDB' 
Select * from Table1 where Day = '12.01.2010';
TO DISK = 'F:\Program Files\Microsoft SQL Server\MSSQL\Backup\sourceDB.bak' WITH FORMAT

Thank you for your support!

Cheers

Stefan

like image 721
Stefan aus Wien Avatar asked Nov 30 '10 13:11

Stefan aus Wien


2 Answers

If you have access to Management Studio, you can save query results to a file quite easily:

  • Open a query window. One way to do this is by right-clicking on the database name in the Object Explorer.
  • You might want to write the query and run it there first, to test that it is producing the desired results.
  • When you are ready to run the query and save it to file, on the menu choose Query, then Results To, and finally Results to File.
  • Now, when you run the query (F5), you will get a dialog box to indicate the filename and folder to save the data.

That's all there is to it.

like image 166
DOK Avatar answered Oct 29 '22 22:10

DOK


The backup command is used to backup entire databases into a proprietary format.

To store the result of a query in a file, check out the bcp utility. This allows you to run a query and store the result in a text file. One example:

bcp "SELECT * FROM Northwind.dbo.Customers" queryout "c:\text.txt" -c -T -x
like image 28
Andomar Avatar answered Oct 29 '22 23:10

Andomar