Skip navigation

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

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


TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish