5

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.