Pages

2011-03-31

SQL Server: DB Restore

  • Restoring a DB:
RESTORE DATABASE AdventureWorks
FROM AdWorksDevice
WITH RECOVERY
  • Restoring Differential Backups in Simple Recovery Mode
RESTORE DATABASE AdventureWorks
FROM DISK='D:\SQLBackups\AWFull.bak'
WITH NORECOVERY -- We aren't done with our Restore process
 Then
RESTORE DATABASE AdventureWorks
FROM DISK='D:\SQLBackups\AWDiff.bak'
WITH NORECOVERY
Finally
RESTORE DATABASE AdventureWorks
WITH RECOVERY

SQL Server: DB Backup - Part 2

  • Creating a Backup Device
EXEC sp_adddumpdevice @devtype='disk',
@logicalname='AWBackup',
@physicalname='D:\backup\AWBackup1.bak'

EXEC sp_adddumpdevice @devtype='tape',
@logicalname='AWTapeBackup',
@physicalname='\\.\tape0'
  • Removing a Backup Device
Sp_dropdevice @logicalname='AWBackup'  
This command only removes the backup device definition—it doesn’t automatically delete the files contained therein. Use this command to remove all the files in addition tothe device definition: 
Sp_dropdevice @logicalname='AWBackup', @devfile='DELFILE'
  • Managing Backup Devices
BACKUP DATABASE AdventureWorks
TO AWBackup
WITH EXPIREDATE = '12/05/2007'
or
BACKUP DATABASE AdventureWorks
TO AWBackup
WITH RETAINDAYS=7
The first option causes that specific backup to be removed after December 5, 2007at midnight. The second option forces that particular backup to be
removed after seven days have passed.

2011-03-10

SQL Server: DB Backup

  • Transaction log backup:
USE [master]
BACKUP LOG [AdventureWorks]
TO DISK = N'C:\AdventureWorks_LOG.TRN' WITH NOFORMAT,
NOINIT,
NAME = N'AdventureWorks-
Transaction log backup.BAK'
GO
  • Backup data file:
USE [master]
BACKUP DATABASE [AdventureWorks]
TO DISK = N'C:\AdventureWorks_DATAS.BAK' WITH NOFORMAT,
NOINIT,
NAME = N'AdventureWorks-Backup data file.BAK'
GO
  • Differential Backup
 In this example, AW.dif contains all the changes made since the last full backup. You can use it during the restore process in addition to transaction log backups. First, restore the full backup, then restore the latest differential, then restore any transaction logs that
follow it.
BACKUP DATABASE Adventureworks TO DISK = 'D:\data\AW.dif' WITH DIFFERENTIAL,INIT
  • Mirrored Backup (SQL Server 2005)
BACKUP DATABASE AdventureWorks TO DISK='D:\data\AW.bak
MIRROR TO DISK = 'E:\data\AW.bak'
MIRROR TO DISK = 'F:\data\AW.bak'
WITH INIT,CHECKSUM,CONTINUE_ON_ERROR
  •  BATCH Script
sqlcmd
-S MYSERVER\SQLEXPRESSINSTANCENAME -U sa -P MYPASSWORD
-Q "BACKUP DATABASE [AdventureWorks] TO DISK = N'C:\AdventureWorks_DATAS.BAK'
WITH NOFORMAT, NOINIT,
NAME = N'AdventureWorks-Backup.BAK'"
Or
SET J=%date:~-10,2%
SET A=%date:~-4%
SET M=%date:~-7,2%
SET H=%time:~0,2%
SET MN=%time:~3,2%
SET S=%time:~-5,2%

IF "%time:~0,1%"==" " SET H=0%HEURE:~1,1%

SET REPERTOIRE=C:\Repertoire_Sauvegarde\

SET FICHIER=%REPERTOIRE%\Nom_de_mon_fichier_%J%_%M%_%A%_A_%H%_%MN%_%S%.bak

IF NOT exist "%REPERTOIRE%" md "%REPERTOIRE%"

cd C:\Program Files\Microsoft SQL Server\90\Tools\Binn

sqlcmd -S NOM_DU_SERVEUR\SQLEXPRESS -Q "BACKUP DATABASE NOM_DE_LA BASE TO DISK = N'%FICHIER%' WITH INIT, NAME = N'Sauvegarde automatique de la base de données', STATS = 1" 

2011-03-08

C++: Using Inserter Iterators

An inserter iterator is an iterator that can add new elements to the sequence containers vector < T > ,
deque < T > , and list < T > . There are three templates that create inserter iterators:
  •  back_insert_iterator < T > — inserts elements at the end of a container of type T . The container
    must provide the push_back() function for this to work.
  • front_insert_iterator < T > — inserts elements at the beginning of a container of type T .
    This depends on push_front() being available for the container.
  • insert_iterator < T > — inserts elements starting at a specifi ed position within a container
    of type T . This requires that the container has an insert() function that accepts an iterator
    as the fi rst argument, and an item to be inserted as the second argument.
The constructors for the fi rst two types of inserter iterators expect a single argument specifying the
container in which elements are to be inserted. For example:
list < int > numbers;
front_insert_iterator < list < int > > iter(numbers);
*iter = 99;  // front_inserter(numbers) = 99;
The constructor for an insert_iterator < T > iterator requires two arguments:
insert_iterator < vector < int > > iter_anywhere(numbers, numbers.begin());
The second argument to the constructor is an iterator specifying where data is to be inserted — the
start of the sequence, in this instance. You can use this iterator in exactly the same way as
the previous one. Here ’ s how you could insert a series of values into a vector container using this iterator:
for(int i = 0 ; i < 100 ; i++)
*iter_anywhere = i + 1;
This loop inserts the values from 1 to 100 in the numbers container at the beginning. After
executing this, the fi rst 100 elements will be 100 , 99 , and so on, through to 1 .
You could also use the inserter() function to achieve the same result:
for(int i = 0 ; i < 100 ; i++)
inserter(numbers, numbers.begin()) = i + 1;
The first argument is the container, and the second is an iterator identifying the
position where data is to be inserted. The inserter iterators can be used in conjunction with
the copy() algorithm in a particularly useful way. Here ’ s how you could read values from cin and
transfer them to a list < T > container:
list < double > values;
cout < < "Enter a series of values separated by spaces"
< < " followed by Ctrl+Z or a letter to end:" < < endl;
istream_iterator < double > input(cin), input_end;
copy(input, input_end, back_inserter < list < double > > (values));