How to insert the results of a DBCC indexdefrag execution into a table

Source: Internet
Author: User
Tags goto

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.