zoukankan      html  css  js  c++  java
  • SQL SERVER – Restore Database Backup using SQL Script (T-SQL)

    SQL SERVER – Restore Database Backup using SQL Script (T-SQL)

    In this blog post we are going to learn how to restore database backup using T-SQL script. We have already database which we will use to take a backup first and right after that we will use it to restore to the server. Taking backup is an easy thing, but I have seen many times when a user tries to restore the database, it throws an error.

    Step 1: Retrive the Logical file name of the database from backup.

    1
    2
    3
    RESTORE FILELISTONLY
    FROM DISK = 'D:BackUpYourBaackUpFile.bak'
    GO

    Step 2: Use the values in the LogicalName Column in following Step.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ----Make Database to single user Mode
    ALTER DATABASE YourDB
    SET SINGLE_USER WITH
    ROLLBACK IMMEDIATE
     
    ----Restore Database
    RESTORE DATABASE YourDB
    FROM DISK = 'D:BackUpYourBaackUpFile.bak'
    WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
    MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'
     
    /*If there is no error in statement before database will be in multiuser
    mode.
    If error occurs please execute following command it will convert
    database in multi user.*/
    ALTER DATABASE YourDB SET MULTI_USER
    GO

    如果是还原数据库本身的话,sql server 2014里面步骤1不需要,

    然后步骤2里面的restore命令,只需要with replace,不需要指定move to

    RESTORE cannot process database, because it is in use by this session

    Msg 3102, Level 16, State 1, Line 8
    RESTORE cannot process database '' because it is in use by this session. It is recommended that the master database be used when performing this operation.
    Msg 3013, Level 16, State 1, Line 8
    RESTORE DATABASE is terminating abnormally.

    把打开的sql窗口里面的数据库,切换成master,就可以执行了

  • 相关阅读:
    大数据课上用spark
    Python 机器学习及实践 Codeing 模型实用技巧 (特征提升 模型正则化 模型检测 超参数搜索)
    学习网站保存
    Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras
    卡尔曼滤波的总结
    MATLAB在一张图上画出多条曲线
    数据库的索引和优化
    线程进程
    static关键字
    单例模式
  • 原文地址:https://www.cnblogs.com/chucklu/p/14714688.html
Copyright © 2011-2022 走看看