Search

Monday, November 8, 2010

Rebuild the log File

1. From a query window, set the status so that you can update the system tables by running the following query:

use Master
go
sp_configure " allow updates", 1
go

reconfigure with override
go

2. Then set the status of the DB that is giving you the problem (XXXXX) into
Emergency Mode by running the following query:

update sysdatabases set status = 32768 where name = ''
go
checkpoint
go

shutdown with nowait
go

3. Go into the data directory e.g. (MSSQL\DATA) and rename the log file associated the DB in question (XXXX.ldf) to some temporary name, such as XXXX.TMP.

4. Exit the query window.

5. Then start up SQL Server from a DOS command window by issuing:

sqlservr -c -T3608 -T4022.

6. Bring up another query window and verify that the DB is in emergency mode by issuing:

Select Name, Status from Sysdatabases where name = ''

7. Verify that the status is 32768. If it is, then issue the query:

dbcc traceon(3604)
dbcc rebuild_log('','') <--- You will need the quotation
marks

REBUILD_LOG should take less than 5 minutes even on a very large database. It should complete with the message DBCC execution completed

8. Take the database out of bypass recovery mode by issuing the command
update sysdatabases set status = 0 where name = ''

9. Exit the query window and then shutdown (Ctrl-C in the DOS window) and restart SQL server. Verify the status of the database by running DBCC CHECKDB on the database.

Inserting DBCC CHECKDB Results Into A Table

Here is the table script (this script is for the SQL Server 2005 CHECKDB output)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[CheckDBResult](
[ServerName] [varchar](100)
COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Error] [int]
NULL,
[Level] [int]
NULL,
[State] [int]
NULL,
[MessageText] [varchar](7000)
COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[RepairLevel] [int]
NULL,
[Status] [int]
NULL,
[DbId] [int]
NULL,
[Id] [int]
NULL,
[IndId] [int]
NULL,
[PartitionId] [int]
NULL,
[AllocUnitId] [int]
NULL,
[File] [int]
NULL,
[Page] [int]
NULL,
[Slot] [int]
NULL,
[RefFile] [int]
NULL,
[RefPage] [int]
NULL,
[RefSlot] [int]
NULL,
[Allocation] [int]
NULL,
[insert_date] [datetime]
NOT NULL CONSTRAINT [DF_CheckDBResult_insert_date] DEFAULT (getdate())
)
ON [PRIMARY]
GO
SET ANSI_PADDING OFF
Here is the CREATE PROCEDURE script that utilizes the CHECKDB 2005 output and here is the T-SQL syntax to execute the procedure: EXEC mDBMaintenance ‘Lumiere’ <- server name to run the script against
EXEC Admin_GetCheckDBResults ’server name’
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
CREATE PROCEDURE [dbo].[Admin_GetCheckDBResults]
@ServerName
VARCHAR(100)

AS
DECLARE
@Database SYSNAME

– Remove Previous Weeks Results
TRUNCATE TABLE DBA_Admin.dbo.CheckDBResult

DECLARE cDatabases CURSOR FOR
SELECT [name] from master..sysdatabases AS sdb
WHERE sdb.[name] NOT IN (‘tempdb’)
OPEN cDatabases
FETCH FROM cdatabases INTO @Database
WHILE ( @@FETCH_STATUS = 0)
BEGIN
SET NOCOUNT ON


INSERT INTO DBA_Admin.dbo.CheckDBResult(
Error,
[Level],
[State],
MessageText,
RepairLevel,
[Status],
[DbId],
Id,
IndId,
PartitionId, — specific to
SQL Server 2005, remove for SQL Server 2000 results
AllocUnitId, — specific to SQL Server 2005, remove for
SQL Server 2000 results
[File],
Page,
Slot,
RefFile,
RefPage,
RefSlot,
Allocation
)
EXEC (‘DBCC CHECKDB(”’ + @Database + ”’) WITH TABLERESULTS’)

FETCH FROM cdatabases INTO @Database
END
CLOSE
cDatabases
DEALLOCATE cDatabases

– Remove all details except CHECKDB information
DELETE
FROM
DBA_Admin.dbo.CheckDBResult
WHERE MessageText NOT LIKE ‘CHECKDB%’

– Update server Name
UPDATE DBA_Admin.dbo.CheckDBResult
SET ServerName = @ServerName

– Select Statment To Return Rows
SELECT ServerName,
MessageText,
Error,
[Level],
[State],
RepairLevel,
[Status],
[DbId],
Id,
IndId,
PartitionId,
– specific to SQL Server 2005, remove for SQL Server 2000 results
AllocUnitId,
– specific to SQL Server 2005, remove for SQL Server 2000 results
[File],
Page,
Slot,
RefFile,
RefPage,
RefSlot,
Allocation,
Insert_Date
FROM DBA_Admin.dbo.CheckDBResult
GO
Now as you can see I only want to see the last line in the DBCC CHECKDB command… however, you can modify it keep all the detail from the execution. I hope this code helps and let us know how you are using it!

Friday, November 5, 2010

Comma delimited string to table

CREATE FUNCTION [dbo].[ufn_ConvCSVToTable] ( @strInput VARCHAR(8000) )
RETURNS @tblTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN

DECLARE @strTemp VARCHAR(10)

WHILE LEN(@strInput) > 0
BEGIN
SET @ strTemp = LEFT(@strInput,
ISNULL(NULLIF(CHARINDEX(',', @ strInput) - 1, -1),
LEN(@strInput)))
SET @ strInput = SUBSTRING(@strInput,
ISNULL(NULLIF(CHARINDEX(',', @ strInput), 0),
LEN(@strInput)) + 1, LEN(@strInput))

INSERT INTO @ tblTable ( [String] )
VALUES ( @ strTemp)
END

RETURN
END
GO

Thursday, November 4, 2010

How to recover a corrupt master MSSQL database.

Recently we had an issue with a client that there master database was corrupt and MSSQL would not start. We had an old backup of the master database and the .mdf and .ldf of the client databases only. Below I have listed out the steps we took to recover the master database and the client databases.


- We started sqlservr.exe as an application using the trace flag -T3608 and it gave the error:

Error: 9003, Severity: 20, State: 1.

Cannot recover the master database. Exiting.

- We then proceeded with reinstallation of sql server for a named intance:NEW. and also applied sp3.

- Started sqlservr.exe -c -m -snew

- Restore database master from disk = 'D:\SQLDATAold\mssql\BACKUP\master_db_200503130200.BAK' with replace

- Started sqlservr.exe -T3608

- Checked the consistency of the Master database using: dbcc checkdb ('MASTER') - zero consistencies

- Next detached the Model database :sp_detach_db 'model'

- Renamed the new instance model db files

- Attached the Model database using: sp_attach_db 'model','D:\sqldata\MSSQL$NEW\Data\model.mdf','D:\sqldata\MSSQL$NEW\Data\modellog.ldf'

- Then took care of the Temp db by : Alter database tempdb modify file (name= 'tempdev', filename = 'D:\sqldata\MSSQL$NEW\Data\tempdb.mdf')

Alter database tempdb modify file (name= 'templog', filename = 'D:\sqldata\MSSQL$NEW\Data\templog.ldf')

- Detached the MSDB database: sp_detach_db 'msdb' and attached it back pointing to the right location:

sp_attach_db 'msdb','D:\sqldata\MSSQL$NEW\Data\msdbdata.mdf', 'D:\sqldata\MSSQL$NEW\Data\msdblog.ldf'

- Then changed the server name by executing the following commands

- select @@servername

- sp_dropserver 'BTLAPP'

- sp_addserver 'BTLAPP\NEW', 'LOCAL'

- Attached the DOCUWARE database and checked its consistencies: dbcc checkdb ('DOCUWARE') - zero consistencies


This worked really well the only drawback is the new instance name of MSSQL. We had to change the ODBC on every client workstation to point to the new name.