Friday, December 26, 2008

SQL SERVER - FIX : ERROR : Msg 3159, Level 16, State 1, Line 1 - Msg 3013, Level 16, State 1, Line 1

SQL SERVER - FIX : ERROR : Msg 3159, Level 16, State 1, Line 1 - Msg 3013, Level 16, State 1, Line 1

While moving some of the script from SQL SERVER 2000 to SQL SERVER 2005 our migration team faced following error.

Msg 3159, Level 16, State 1, Line 1
The tail of the log for the database "AdventureWorks" has not been backed up.
Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose.
Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Following is the similar script using AdventureWorks samples database as example. This scripts works perfectly fine with SQL SERVER 2000. It gives the error in SQL SERVER 2005.

RESTORE DATABASE AdventureWorks
FROM DISK = 'C:BackupAdventureworks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:Data',
MOVE 'AdventureWorks_Log' TO 'C:Data'

The reason of error is already explained in the error detail. Requirement of backing up tail of the log for the database can be overridden by using RESTORE command. Most of the WITH clause statements can be used in combination with the others. Change the syntax of above script with addition of REPLACE in WITH clause.

ALTER DATABASE AdventureWorks
SET SINGLE_USER WITH
ROLLBACK
IMMEDIATE

RESTORE DATABASE AdventureWorks
FROM DISK = 'C:BackupAdventureworks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:\Data\datafile.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\Data\logfile.ldf',

Additional Note:
Once you have used REPLACE Syntax you will not able to add/restore any
other transaction or differential blog after that. However another full
back will work just normal.
REPLACE

Reference : Pinal Dave (http://www.SQLAuthority.com),BOL

1 comment:

Unknown said...

Oh, I am interested in similar solutions, too. Look at the mdb file repair utility, it parses affected files, backup copies are no longer needed