Lorenzo Benaglia


Storie di un SQLlaro mannaro...
Archivio Posts
Anno 2010

Anno 2009

Anno 2008

Anno 2007

Anno 2006

Anno 2005
Calendario
aprile 2024
lmmgvsd
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Manipolare l'output del comando DBCC CHECKDB

Ieri sul newsgroup microsoft.public.it.sql David ha chiesto se esiste un modo di eseguire il comando DBCC CHECKDB e di manipolarne l'output mediante comandi SQL.
Come riportano i Books Online, DBCC CHECKDB verifica l'integrità logica, strutturale e di allocazione di tutti gli oggetti del database specificato eseguendo le operazioni seguenti:

• Esecuzione di DBCC CHECKALLOC sul database
• Esecuzione di DBCC CHECKTABLE su ogni tabella e vista del database
• Convalida dei dati di Service Broker nel database
• Esecuzione di DBCC CHECKCATALOG sul database
• Convalida del contenuto di ogni vista indicizzata nel database

Osservando le opzioni disponibili i Books Online non riportano TABLERESULTS (presente ad esempio nel comando DBCC SHOWCONTIG) utilizzata per visualuzzare i risultati come set di righe.
Ad ogni modo io ricodavo di aver letto da qualche parte che questa opzione fosse disponibile anche per il comando DBCC CHECKDB così ho provato a scrivere il seguente esempio:

USE tempdb;
GO

/* Definisco la tabella che andrà a contenere l'output generato dal comando DBCC CHECKDB */
CREATE TABLE dbo.#Output(
Error int NOT NULL,
[Level] int NOT NULL,
State int NOT NULL,
MessageText nvarchar(256) NOT NULL,
RepairLevel int NULL,
Status int NOT NULL,
DbId int NOT NULL,
ObjectId int NOT NULL,
IndexId int NOT NULL,
PartitionId int NOT NULL,
AllocUnitId int NOT NULL,
[File] int NOT NULL,
Page int NOT NULL,
Slot int NOT NULL,
RefFile int NOT NULL,
RefPage int NOT NULL,
RefSlot int NOT NULL,
Allocation int NOT NULL
);
GO

/* Popolo la tabella di output */
INSERT dbo.#Output
EXEC('DBCC CHECKDB(master) WITH TABLERESULTS');
GO

/* Output:

DBCC execution completed. If DBCC printed error messages, contact your
system administrator.

(131 row(s) affected)

*/

/* Esempio di query */
SELECT MessageText
FROM dbo.#Output
WHERE Error = 8997;
GO

/* Output:

MessageText
------------------------------------------------------------------------
Service Broker Msg 9675, State 1: Message Types analyzed: 14.
Service Broker Msg 9676, State 1: Service Contracts analyzed: 6.
Service Broker Msg 9667, State 1: Services analyzed: 3.
Service Broker Msg 9668, State 1: Service Queues analyzed: 3.
Service Broker Msg 9669, State 1: Conversation Endpoints analyzed: 0.
Service Broker Msg 9674, State 1: Conversation Groups analyzed: 0.
Service Broker Msg 9670, State 1: Remote Service Bindings analyzed: 0.
Service Broker Msg 9675, State 1: Message Types analyzed: 14.
Service Broker Msg 9676, State 1: Service Contracts analyzed: 6.
Service Broker Msg 9667, State 1: Services analyzed: 3.
Service Broker Msg 9668, State 1: Service Queues analyzed: 3.
Service Broker Msg 9669, State 1: Conversation Endpoints analyzed: 0.
Service Broker Msg 9674, State 1: Conversation Groups analyzed: 0.
Service Broker Msg 9670, State 1: Remote Service Bindings analyzed: 0.

(14 row(s) affected)

*/

/* Pulizia */
DROP TABLE dbo.#Output;


La struttura della tabella l'ho ottenuta osservando l'output generato dal comando DBCC CHECKDB...WITH TABLERESULTS e non assicuro che i data type siano corretti e dimensionati opportunamente, quindi è possibile che sia necessario apportare alcuni aggiustamenti.
Categoria: SQL Server
martedì, 03 ott 2006 Ore. 10.50
Statistiche
  • Views Home Page: 933.575
  • Views Posts: 698.360
  • Views Gallerie: 12.666.015
  • n° Posts: 300
  • n° Commenti: 314
Mappa





















Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003