Skip to main content

AddToAny

Share/Save

SQL Server: Backup and Restore Database using T-SQL

Its very easy to take Backup and Restore your Database using T-SQL even if you are not a DBA. In one of the client application, I wanted to take periodic Backup without logging in to SQL Server Management Studio. Here in this small tutorial I will try to elaborate the procedure of taking database backup using T-SQL.

1. Create a Database

CREATE DATABASE PracticeDB

2. Take Backup of the Database

BACKUP DATABASE PracticeDB
TO DISK='C:\PracticeD.bak'
WITH DESCRIPTION='Database Backup',
INIT,
PASSWORD='TryAgain'

(We can use many Options with 'WITH' in the above query)

Description is the free text used to describe the Backup.

INIT Overwrites the Backup with the same name. If we dont use INIT the backup will be appended to the previous backup if any present.

PASSWORD is used to keep secure your Backup. On Restore you will be prompt to enter this password.

Now you have sucessfully taken Backup of the Database

3. Restore the database from the previously taken backup

RESTORE DATABASE practiceDB
FROM DISK='C:\PracticeD.bak'
WITH PASSWORD='TryAgain'

Thank you very much for the

Thank you very much for the excellent and useful subject.  

restore

sir

 I took backup a database in sql server 2005 using MS SQL server Management studio in windows 7 . But i need to restore the backup file into sql server 2005 in windows xp sp2 .. I tried for a couple of days .. But i Cannot get the restore..

It causes os error 3..

What can i do..

It is very urgent..

plz replay..

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr><br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong><font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <csharp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <sql>, <tsql>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Lines and paragraphs break automatically.

More information about formatting options