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

Neil Pike

January 24, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. 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 :-

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


Another is dbcc inputbuffer :-

DROP TABLE #dbc
go
create table #dbc(c1 varchar(15), c2 int, c3 varchar(255))

insert #dbc EXEC('dbcc inputbuffer(7)')


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

-- Note that all quotes are single quotes except the ones on the -Q option, which are double quotes
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

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like