Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Wednesday, March 28, 2012

restore databases in T-SQL.

--Drop database
if exists (select * from master.dbo.sysdatabases where name='Test')
drop database Test
GO
--Drop device
if exists (select * from master.dbo.sysdevices where name='mydiskdump')
exec master.dbo.sp_dropdevice mydiskdump
GO
--Create Test database
CREATE DATABASE Test
ON
( NAME = Test_dat,
FILENAME = 'c:\Test.mdf',
SIZE = 5,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Test_log',
FILENAME = 'c:\Test.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
--add device
EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
GO
CREATE TABLE Test.dbo.tblTest (
col1 char(1)
)
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values ('a')
GO
--whole backup 1
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('b')
GO
--differential backup 1
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
--whole backup 2
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('c')
GO
--differential backup 2
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('d')
GO
Am I correct in creating the database and the way of backup?
Would you teach me the senarios that restore the database to 'a','b','c','d'
states?
Thanks in advance.You'll need to add another backup to backup 'd', but something like this
should do:
--Drop database
if exists (select * from master.dbo.sysdatabases where name='Test')
drop database Test
GO
--Drop device
if exists (select * from master.dbo.sysdevices where name='mydiskdump')
exec master.dbo.sp_dropdevice mydiskdump
GO
--Create Test database
CREATE DATABASE Test
ON
( NAME = Test_dat,
FILENAME = 'c:\Test.mdf',
SIZE = 5,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Test_log',
FILENAME = 'c:\Test.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
--add device
EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
GO
CREATE TABLE Test.dbo.tblTest (
col1 char(1)
)
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values ('a')
GO
--whole backup 1
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('b')
GO
--differential backup 1
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
--whole backup 2
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('c')
GO
--differential backup 2
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('d')
GO
--whole backup 3
BACKUP DATABASE Test TO mydiskdump
go
restore headeronly from mydiskdump
go
restore database Test from mydiskdump with file = 1, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 1, norecovery
restore database Test from mydiskdump with file = 2, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 3, norecovery
restore database Test from mydiskdump with file = 4, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 5, recovery
select * from Test.dbo.tblTest
go
Note that dump devices can store multiple backup sets. You can get the
backup sets that a dump device contains using the restore headeronly command
which lets you know which backup sets to restore. I'd suggest you consider
naming your backup sets though as this can help work out which backups to
restore as well..
HTH
Regards,
Greg Linwood
SQL Server MVP
"haode" <haode@.hao.com> wrote in message
news:%23eNTtec9DHA.1504@.TK2MSFTNGP12.phx.gbl...
> --Drop database
> if exists (select * from master.dbo.sysdatabases where name='Test')
> drop database Test
> GO
> --Drop device
> if exists (select * from master.dbo.sysdevices where name='mydiskdump')
> exec master.dbo.sp_dropdevice mydiskdump
> GO
> --Create Test database
> CREATE DATABASE Test
> ON
> ( NAME = Test_dat,
> FILENAME = 'c:\Test.mdf',
> SIZE = 5,
> MAXSIZE = 50,
> FILEGROWTH = 5 )
> LOG ON
> ( NAME = 'Test_log',
> FILENAME = 'c:\Test.ldf',
> SIZE = 5MB,
> MAXSIZE = 25MB,
> FILEGROWTH = 5MB )
> GO
> --add device
> EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
> GO
> CREATE TABLE Test.dbo.tblTest (
> col1 char(1)
> )
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values ('a')
> GO
> --whole backup 1
> BACKUP DATABASE Test TO mydiskdump
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('b')
> GO
> --differential backup 1
> BACKUP DATABASE Test
> TO mydiskdump
> WITH DIFFERENTIAL
> GO
> --whole backup 2
> BACKUP DATABASE Test TO mydiskdump
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('c')
> GO
> --differential backup 2
> BACKUP DATABASE Test
> TO mydiskdump
> WITH DIFFERENTIAL
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('d')
> GO
> Am I correct in creating the database and the way of backup?
> Would you teach me the senarios that restore the database to
'a','b','c','d'
> states?
> Thanks in advance.
>

restore databases in T-SQL.

--Drop database
if exists (select * from master.dbo.sysdatabases where name='Test')
drop database Test
GO
--Drop device
if exists (select * from master.dbo.sysdevices where name='mydiskdump')
exec master.dbo.sp_dropdevice mydiskdump
GO
--Create Test database
CREATE DATABASE Test
ON
( NAME = Test_dat,
FILENAME = 'c:\Test.mdf',
SIZE = 5,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Test_log',
FILENAME = 'c:\Test.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
--add device
EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
GO
CREATE TABLE Test.dbo.tblTest (
col1 char(1)
)
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values ('a')
GO
--whole backup 1
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('b')
GO
--differential backup 1
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
--whole backup 2
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('c')
GO
--differential backup 2
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
delete Test.dbo.tblTest values
insert into Test.dbo.tblTest values('d')
GO
Am I correct in creating the database and the way of backup?
Would you teach me the senarios that restore the database to 'a','b','c','d'
states?
Thanks in advance.You'll need to add another backup to backup 'd', but something like this
should do:
--Drop database
if exists (select * from master.dbo.sysdatabases where name='Test')
drop database Test
GO
--Drop device
if exists (select * from master.dbo.sysdevices where name='mydiskdump')
exec master.dbo.sp_dropdevice mydiskdump
GO
--Create Test database
CREATE DATABASE Test
ON
( NAME = Test_dat,
FILENAME = 'c:\Test.mdf',
SIZE = 5,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Test_log',
FILENAME = 'c:\Test.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
--add device
EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
GO
CREATE TABLE Test.dbo.tblTest (
col1 char(1)
)
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values ('a')
GO
--whole backup 1
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('b')
GO
--differential backup 1
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
--whole backup 2
BACKUP DATABASE Test TO mydiskdump
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('c')
GO
--differential backup 2
BACKUP DATABASE Test
TO mydiskdump
WITH DIFFERENTIAL
GO
delete Test.dbo.tblTest
insert into Test.dbo.tblTest values('d')
GO
--whole backup 3
BACKUP DATABASE Test TO mydiskdump
go
restore headeronly from mydiskdump
go
restore database Test from mydiskdump with file = 1, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 1, norecovery
restore database Test from mydiskdump with file = 2, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 3, norecovery
restore database Test from mydiskdump with file = 4, recovery
select * from Test.dbo.tblTest
restore database Test from mydiskdump with file = 5, recovery
select * from Test.dbo.tblTest
go
Note that dump devices can store multiple backup sets. You can get the
backup sets that a dump device contains using the restore headeronly command
which lets you know which backup sets to restore. I'd suggest you consider
naming your backup sets though as this can help work out which backups to
restore as well..
HTH
Regards,
Greg Linwood
SQL Server MVP
"haode" <haode@.hao.com> wrote in message
news:%23eNTtec9DHA.1504@.TK2MSFTNGP12.phx.gbl...
> --Drop database
> if exists (select * from master.dbo.sysdatabases where name='Test')
> drop database Test
> GO
> --Drop device
> if exists (select * from master.dbo.sysdevices where name='mydiskdump')
> exec master.dbo.sp_dropdevice mydiskdump
> GO
> --Create Test database
> CREATE DATABASE Test
> ON
> ( NAME = Test_dat,
> FILENAME = 'c:\Test.mdf',
> SIZE = 5,
> MAXSIZE = 50,
> FILEGROWTH = 5 )
> LOG ON
> ( NAME = 'Test_log',
> FILENAME = 'c:\Test.ldf',
> SIZE = 5MB,
> MAXSIZE = 25MB,
> FILEGROWTH = 5MB )
> GO
> --add device
> EXEC master.dbo.sp_addumpdevice 'disk', 'mydiskdump', 'c:\dump.bak'
> GO
> CREATE TABLE Test.dbo.tblTest (
> col1 char(1)
> )
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values ('a')
> GO
> --whole backup 1
> BACKUP DATABASE Test TO mydiskdump
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('b')
> GO
> --differential backup 1
> BACKUP DATABASE Test
> TO mydiskdump
> WITH DIFFERENTIAL
> GO
> --whole backup 2
> BACKUP DATABASE Test TO mydiskdump
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('c')
> GO
> --differential backup 2
> BACKUP DATABASE Test
> TO mydiskdump
> WITH DIFFERENTIAL
> GO
> delete Test.dbo.tblTest values
> insert into Test.dbo.tblTest values('d')
> GO
> Am I correct in creating the database and the way of backup?
> Would you teach me the senarios that restore the database to
'a','b','c','d'
> states?
> Thanks in advance.
>sql

Tuesday, March 20, 2012

Restore Database action not responding

I followed the "Restore Database to a new Server" process using Enterprise
Manager and T-SQL using the WITH MOVE option. Both processes seem to be
hanging.
In EM, the Restore Progress bar shows it is restoring but no progress is made.
In T-SQL, I just receive Executing Query Batch...
Any ideas on what is going on? What do I need to look at?
Thanks-
Deb
Deb
RESTORE may need to create the database before the restore actually begins
and this can take considerable time. Is this a large database?
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>I followed the "Restore Database to a new Server" process using Enterprise
> Manager and T-SQL using the WITH MOVE option. Both processes seem to be
> hanging.
> In EM, the Restore Progress bar shows it is restoring but no progress is
> made.
> In T-SQL, I just receive Executing Query Batch...
> Any ideas on what is going on? What do I need to look at?
> Thanks-
> Deb
>
> --
> Deb
|||Ahhh. I will let it run overnight then. It is almost 14GB.
Deb
"Dan Guzman" wrote:

> RESTORE may need to create the database before the restore actually begins
> and this can take considerable time. Is this a large database?
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Deb" <Deb@.discussions.microsoft.com> wrote in message
> news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>
>
|||I wouldn't expect this to take more than a few hours, even on modest
hardware. You can get an idea of how long it might take by checking out the
performance monitor metric: physical disk/bytes per sec.
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:A980EE61-E755-4837-90CE-728E794809C5@.microsoft.com...[vbcol=seagreen]
> Ahhh. I will let it run overnight then. It is almost 14GB.
> --
> Deb
>
> "Dan Guzman" wrote:

Restore Database action not responding

I followed the "Restore Database to a new Server" process using Enterprise
Manager and T-SQL using the WITH MOVE option. Both processes seem to be
hanging.
In EM, the Restore Progress bar shows it is restoring but no progress is mad
e.
In T-SQL, I just receive Executing Query Batch...
Any ideas on what is going on? What do I need to look at?
Thanks-
Deb
DebRESTORE may need to create the database before the restore actually begins
and this can take considerable time. Is this a large database?
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>I followed the "Restore Database to a new Server" process using Enterprise
> Manager and T-SQL using the WITH MOVE option. Both processes seem to be
> hanging.
> In EM, the Restore Progress bar shows it is restoring but no progress is
> made.
> In T-SQL, I just receive Executing Query Batch...
> Any ideas on what is going on? What do I need to look at?
> Thanks-
> Deb
>
> --
> Deb|||Ahhh. I will let it run overnight then. It is almost 14GB.
--
Deb
"Dan Guzman" wrote:

> RESTORE may need to create the database before the restore actually begins
> and this can take considerable time. Is this a large database?
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Deb" <Deb@.discussions.microsoft.com> wrote in message
> news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>
>|||I wouldn't expect this to take more than a few hours, even on modest
hardware. You can get an idea of how long it might take by checking out the
performance monitor metric: physical disk/bytes per sec.
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:A980EE61-E755-4837-90CE-728E794809C5@.microsoft.com...[vbcol=seagreen]
> Ahhh. I will let it run overnight then. It is almost 14GB.
> --
> Deb
>
> "Dan Guzman" wrote:
>

Restore Database action not responding

I followed the "Restore Database to a new Server" process using Enterprise
Manager and T-SQL using the WITH MOVE option. Both processes seem to be
hanging.
In EM, the Restore Progress bar shows it is restoring but no progress is made.
In T-SQL, I just receive Executing Query Batch...
Any ideas on what is going on? What do I need to look at?
Thanks-
Deb
--
DebRESTORE may need to create the database before the restore actually begins
and this can take considerable time. Is this a large database?
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>I followed the "Restore Database to a new Server" process using Enterprise
> Manager and T-SQL using the WITH MOVE option. Both processes seem to be
> hanging.
> In EM, the Restore Progress bar shows it is restoring but no progress is
> made.
> In T-SQL, I just receive Executing Query Batch...
> Any ideas on what is going on? What do I need to look at?
> Thanks-
> Deb
>
> --
> Deb|||Ahhh. I will let it run overnight then. It is almost 14GB.
--
Deb
"Dan Guzman" wrote:
> RESTORE may need to create the database before the restore actually begins
> and this can take considerable time. Is this a large database?
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Deb" <Deb@.discussions.microsoft.com> wrote in message
> news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
> >I followed the "Restore Database to a new Server" process using Enterprise
> > Manager and T-SQL using the WITH MOVE option. Both processes seem to be
> > hanging.
> >
> > In EM, the Restore Progress bar shows it is restoring but no progress is
> > made.
> >
> > In T-SQL, I just receive Executing Query Batch...
> >
> > Any ideas on what is going on? What do I need to look at?
> >
> > Thanks-
> > Deb
> >
> >
> >
> > --
> > Deb
>
>|||I wouldn't expect this to take more than a few hours, even on modest
hardware. You can get an idea of how long it might take by checking out the
performance monitor metric: physical disk/bytes per sec.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Deb" <Deb@.discussions.microsoft.com> wrote in message
news:A980EE61-E755-4837-90CE-728E794809C5@.microsoft.com...
> Ahhh. I will let it run overnight then. It is almost 14GB.
> --
> Deb
>
> "Dan Guzman" wrote:
>> RESTORE may need to create the database before the restore actually
>> begins
>> and this can take considerable time. Is this a large database?
>> --
>> Hope this helps.
>> Dan Guzman
>> SQL Server MVP
>> "Deb" <Deb@.discussions.microsoft.com> wrote in message
>> news:30CD3ECC-B47B-44EB-A396-F3DA717F1FB2@.microsoft.com...
>> >I followed the "Restore Database to a new Server" process using
>> >Enterprise
>> > Manager and T-SQL using the WITH MOVE option. Both processes seem to
>> > be
>> > hanging.
>> >
>> > In EM, the Restore Progress bar shows it is restoring but no progress
>> > is
>> > made.
>> >
>> > In T-SQL, I just receive Executing Query Batch...
>> >
>> > Any ideas on what is going on? What do I need to look at?
>> >
>> > Thanks-
>> > Deb
>> >
>> >
>> >
>> > --
>> > Deb
>>