Stefan aus Wien
Stefan aus Wien

Reputation: 75

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

Upvotes: 5

Views: 5045

Answers (2)

DOK
DOK

Reputation: 32831

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.

Upvotes: 4

Andomar
Andomar

Reputation: 238048

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

Upvotes: 4

Related Questions