Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. stored procedure for restore .bak file

stored procedure for restore .bak file

Scheduled Pinned Locked Moved Database
databasehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarendraSinghJTV
    wrote on last edited by
    #1

    hi all i have a .bak file created with a stored procedure which takes backup of a database. Now i want to restore that .bak file to the database can anybody help me with a stored procedure. This .bak file is created with the help of backup database databasename to disk = "destination path" with thanks for ur replies in advance

    Regards Narendra Singh (Jindal Tech Ventures)

    V 1 Reply Last reply
    0
    • N NarendraSinghJTV

      hi all i have a .bak file created with a stored procedure which takes backup of a database. Now i want to restore that .bak file to the database can anybody help me with a stored procedure. This .bak file is created with the help of backup database databasename to disk = "destination path" with thanks for ur replies in advance

      Regards Narendra Singh (Jindal Tech Ventures)

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      i have this

      set ANSI_NULLS ON
      set QUOTED_IDENTIFIER ON
      go

      -- Restore a backed up database
      -- Given: the filename and path where the backedup database file is located and the name of the database to restore it as
      -- The entry will be restored and a row placed into oDirect.dbo.tbl_dbref - which keeps track of the databases
      -- The users for the database must also be restored!
      ALTER PROCEDURE [dbo].[sp_RestoreDatabase]

      @dbname char(32), 				-- the database name to restore as
      @filename char(64), @path char(256)		-- the location of the backuped up database file	(on the SQL Server)
      

      AS

      set nocount on

      declare @sql nvarchar(3000)

      execute('sp_ClearDatabaseConnections ' + @dbname)

      -- Restore the database
      select @sql = ' RESTORE DATABASE ' + ltrim(rtrim( @dbname )) + ' FROM DISK = ''' + ltrim(rtrim(@path)) + ltrim(rtrim( @filename )) + ''' '
      select @sql = ltrim(rtrim(@sql)) + ' WITH '
      select @sql = ltrim(rtrim(@sql)) + ' MOVE ''' + 'TNGoedit_Data' + ''' TO ''' + ltrim(rtrim(@path)) + ltrim(rtrim( @dbname )) + '.mdf' + ''' , ' -- logical file name to physical name
      select @sql = ltrim(rtrim(@sql)) + ' MOVE ''' + 'TNGoedit_Log' + ''' TO ''' + ltrim(rtrim(@path)) + ltrim(rtrim( @dbname )) + '_log.ldf' + ''' ' -- logical file name to physical name
      select @sql = ltrim(rtrim(@sql)) + ' ,REPLACE,RECOVERY;'
      --select @sql = ltrim(rtrim(@sql)) + ' MOVE ''' + ltrim(rtrim(@dbname)) + '_Data' + ''' TO ''' + ltrim(rtrim(@path)) + ltrim(rtrim( @dbname )) + '.mdf' + ''' , ' -- logical file name to physical name
      --select @sql = ltrim(rtrim(@sql)) + ' MOVE ''' + ltrim(rtrim(@dbname)) + '_Log' + ''' TO ''' + ltrim(rtrim(@path)) + ltrim(rtrim( @dbname )) + '_log.ldf' + ''' ' -- logical file name to physical name
      print @sql
      execute ( @sql )

      -- Was the command successful or was there a problem
      if ( (select @@Error) = 0 ) begin
      -- Put an entry into oDirect.dbo.tbl_dbRef
      -- execute ( 'sp_DataSet_Save ''' + @xml + ''' ' )

      -- TODO: restore the users
      

      select 'Restore Successful' [Result]
      end
      else begin
      select 'Restore Unsuccessful' [Result]
      end

      You can Simply change it to BAK file

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups