Restore Sql Server using stored procedure

  • Iam using a stored procedure named "RestoreDB" which is as follows

    -----------------------------------------

    USE Master

    GO

    CREATE PROCEDURE RestoreDB

    @DBFILE CHAR(30)

    AS

    exec sp_dboption 'Test', 'offline',  true;

    RESTORE DATABASE Test FROM ISK=@DBFILE">DISK=@DBFILE WITH REPLACE,STATS=2

    exec sp_dboption 'Test', 'offline',  false;

    GO

    ------------------------------------

    * Note Test is the database Iam trying to restoring on & that why Iam trying to make it offline first the perform restore on that.

    This is giving me error i.e.

    "User must be in the master database."

    I dont know how to handle it, can any body tell me how to force a restore on a DB

    Can anybody help me out in this regard.

    Regards

    Mohsin

  • Are u in the master database when you are executing your procedure. ?

    I executed your procedure at my end. Its working perfect.

    Just try with 'Use master' first and then exec your procedure.

    --Kishore

  • Try fully qualifying the sp_dboption command like this:

    master.dbo.sp_dboption.  But I don't know why you have to take the old database offline first. 

    Aunt Kathi Data Platform MVP
    Author of Expert T-SQL Window Functions
    Simple-Talk Editor

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply