Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem: Restore DB right after back it up in Stored Proc

Status
Not open for further replies.

jluost1

Programmer
Jun 8, 2001
78
US
Hi, I would like to programmingly create a new database based on my database template, the new database has to reflect the latest change on the template database.

So, I created the following stored procedure. It first backs-up the template database, then restore it with a new name.

Unfortunately, it doesn't reflect my latest change on the template database even though I immediately back it up before restoring. Can anyone tell me what is missing, or how to force it to restore it with the latest backup?

-----------------------------------------
CREATE PROCEDURE CREATE_WEBDB
@template_name varchar(50), --template database
@newname varchar(50) --New database name
AS

IF NOT EXISTS(SELECT * FROM MASTER..SYSDATABASES WHERE NAME=@newname)

begin
--(variable declaration removed for clarity)

set @back_db_name = 'back_' + @template_name
set @diskname = 'D:\sqldata\backup\' + @template_name + '.bak'


set @full_bk_data_file =@template_name + '_data'
set @full_bk_log_file = @template_name +'_log'

set @full_data_file = 'D:\sqldata\data\' + @newname + '_data.mdf'
set @full_log_file = 'D:\sqldata\data\' + @newname + '_log.ldf'

BACKUP DATABASE @template_name
TO DISK = @diskname

--RESTORE FILELISTONLY
--FROM DISK = @diskname

RESTORE DATABASE @newname
FROM DISK = @diskname

WITH MOVE @full_bk_data_file TO @full_data_file,
MOVE @full_bk_log_file TO @full_log_file

end
--------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top