Me Myself & C#

Manoj Garg’s Tech Bytes – What I learned Today

Archive for the ‘Database’ Category

How to repair a SQL Server 2005 Suspect database

Posted by Manoj Garg on July 17, 2008

Sometimes when you connect to your database server, you may find it in suspect mode. Your database server won’t allow you to perform any operation on that database until the database is repaired.

A database can go in suspect mode for many reasons like improper shutdown of the database server, corruption of the database files etc.

To get the exact reason of a database going into suspect mode can be found using the following query,

DBCC CHECKDB (‘YourDBname’) WITH NO_INFOMSGS, ALL_ERRORMSGS

Output of the above query will give the errors in the database.

To repair the database, run the following queries in Query Analyzer,

EXEC sp_resetstatus ‘yourDBname’;

ALTER DATABASE yourDBname SET EMERGENCY

DBCC checkdb(‘yourDBname’)

ALTER DATABASE yourDBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE

DBCC CheckDB (‘yourDBname’, REPAIR_ALLOW_DATA_LOSS)

ALTER DATABASE yourDBname SET MULTI_USER

and you are done. 🙂

lightbulbYou should keep one thing in mind while using the above queries that the repair mode used here , REPAIR_ALLOW_DATA_LOSS, is a one way operation i.e. once the database is repaired all the actions performed by these queries can’t be undone. There is no way to go back to the previous state of the database. So as a precautionary step you should take backup of your database before executing above mentioned queries.

Ha-P Querying…

Posted in Database, SQL Server 2005 | Tagged: , , | 121 Comments »

Truncating Transaction Log in SQLServer 2000

Posted by Manoj Garg on July 17, 2007

Few days back I had a situation when size of my database transcation log was growing too  fast (at a point it reached 60 GB). so I had to find some way to truncate it to some minimal size. After lot of googling, I found how to truncate the transaction log.

Run these two commands in Query Analyser.

  1. BACKUP LOG <<DBName>> WITH TRUNCATE_ONLY
  2. DBCC SHRINKFILE(<<DBName>>_log, <<Desired transaction filesize>> )

Remarks :

  • DBCC SHRINKFILE applies to the files in the current database. If you try to  run this command in a different DB then the DB you want to shrink, SQL Server will give following error

Server: Msg 8985, Level 16, State 1, Line 1
Could not locate file ‘<<DBName>>_log’ in sysfiles.

  • SQL Server uses <<Desired transaction filesize>> to calculate the target size for the entire log; therefore, target_size is the amount of free space in the log after the shrink operation. Target size for the entire log is then translated to target size for each log file. DBCC SHRINKFILE attempts to shrink each physical log file to its target size immediately. If no part of the logical log resides in the virtual logs beyond the log file’s target size, the file is successfully truncated and DBCC SHRINKFILE completes with no messages. However, if part of the logical log resides in the virtual logs beyond the target size, SQL Server frees as much space as possible and then issues an informational message. The message tells you what actions you need to perform to move the logical log out of the virtual logs at the end of the file. (Remarks are taken from MSDN)

References…..

  1. Microsoft Knowledge Base (KB272318)
  2. DBCC SHRINKFILE
Ha-P Querying 🙂

Posted in Database, MS SQL Server 2000 | Tagged: , | 3 Comments »