How to insert the results of a DBCC indexdefrag execution into a table
To insert the results of the DBCC INDEXDEFRAG execution into the table, you receive the following error message "The results of DBCC INDEXDEFRAG cannot be manipulated under user transactions"
--Get data
DECLARE @err int, @src varchar (255), @desc varchar (255)
DECLARE @obj int, @str varchar (1000)
--Log on using Windows identity
SET @str = ' provider=sqloledb.1;integrated security=sspi; Persist security info=false;initial catalog=master;data source= '
--Log in as SQL Server Use this string, note set user name and password
--SET @str = ' provider=sqloledb.1; Persist security info=false;initial Catalog=master;data source=; User Id=sa; Password= '
--Creating objects
EXEC @err =sp_oacreate ' Adodb.recordset ', @obj out
IF @err <>0 GOTO lberr
--Get the data you need
EXEC @err =sp_oamethod @obj, ' open ', null, ' DBCC Indexdefrag (Northwind, Orders, CustomersOrders) ', @str
IF @err <>0 GOTO lberr
CREATE TABLE # (Column0 int,column1 int,column2 int)
INSERT # EXEC @err =sp_oamethod @obj, ' GetRows '
IF @err <>0 GOTO lberr
EXEC @err =sp_oadestroy @obj
Return
Lberr:
EXEC sp_OAGetErrorInfo 0, @src out, @desc out
SELECT CAST (@err as varbinary (4)) as error number,
@src as error source, @desc as error description
Go
--Show results
SELECT * FROM #
DROP TABLE #
Original post Address