Search
  Home
  Articles
  Backup
  Books
  Certification
  FAQ
  Products
  Replication
  Scripts
  Seminars
  Training
  TSQL

MSDN Fourms
Philippine SSUG

  Resume

MHS Enterprises
FilAm Software
AcrylicAcetate.com
Bargain Humidors
Western Humidor



How can I insert the output of a dbcc command into a SQL Server table?

Some DBCC commands support this directly via the "insert into exec" type format, others don't. Examples of both are given.

A dbcc command that works "normally" is useroptions - this is an example from the books-online :-

drop table #tb_setopts
go
CREATE TABLE #tb_setopts (SetOptName varchar(35) NOT NULL ,SetOptValue varchar(35) null)
INSERT INTO #tb_setopts (SetOptName,SetOptValue)
EXEC('dbcc useroptions')
select * from #tb_setopts

Another is dbcc sqlperf :-

CREATE TABLE #TempForLogSpace
(
DBName varchar(32),
LogSize real,
LogSpaceUsed real,
Status int
)
SELECT @sql_command = 'dbcc sqlperf (logspace)'
INSERT INTO #TempForLogSpace EXEC (@sql_command)

One that doesn't is dbcc checkdb. To make this work you'll need to use xp_cmdshell and ISQL as follows :-

DROP TABLE #maint
go
DECLARE @SQLSTR varchar(255)
SELECT @SQLSTR = 'ISQL -E -Q"dbcc checkdb(master)"'
CREATE TABLE #maint (Results varchar(255) NOT NULL)
INSERT INTO #maint(Results) EXEC('master..xp_cmdshell ''ISQL -E -Q"dbcc checkdb(master)"''')
select * from #maint

Michael R. Hotek

All content on this site, except where noted, represents an original work of Michael R. Hotek and is protected by applicable copyright laws. The SQL Server FAQ is the sole work of Neil Pike. No page, portion of a page, or download may be used for commercial purposes in whole or in part without the express, written permission of the applicable author.