Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 reindexing using sp written by Tara

Author  Topic 

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-12-08 : 01:01:20
Hi All,

Here is code by Tara to reindex all tables in database.
I just changed procedure name. from isp_DBCC_DBREINDEX to sp_DBCC_DBREINDEX and placed in master to share by multiple databases.

My problem is it is running for few dbs & failing fr few databases.
I got following error:
There is already an object named '##Indexes' in the database. [SQLSTATE 42S01] (Error 2714). The step failed.

But i can do to fix this.
Do i really need ##indexes, i guess i can change to #indexes?

Thanks




----------------------------------------------------------------------------------------------------
-- OBJECT NAME : isp_DBCC_DBREINDEX
---- INPUTS : @dbName - name of the database
-- OUTPUTS : None
-- DEPENDENCIES : None
--
-- DESCRIPTION : This stored procedure runs DBCC DBREINDEX for each of the indexes in the database.
--
-- EXAMPLES (optional) : EXEC sp_DBCC_DBREINDEX @dbName = 'CDS_Systems'
----------------------------------------------------------------------------------------------------
CREATE PROC sp_DBCC_DBREINDEX
(@dbName SYSNAME)
AS
SET NOCOUNT ON

DECLARE @objName SYSNAME
DECLARE @idxName SYSNAME
DECLARE @SQL NVARCHAR(4000)
DECLARE @ID INT
DECLARE @RowCnt INT

CREATE TABLE ##Indexes
(
Indexes_ID INT IDENTITY(1, 1) NOT NULL,
IndexName SYSNAME NOT NULL,
ObjectName SYSNAME NOT NULL
)

SET @SQL = ''
SET @SQL = @SQL + 'INSERT INTO ##Indexes (IndexName, ObjectName) '
SET @SQL = @SQL + 'SELECT i.name, o.name '
SET @SQL = @SQL + 'FROM ' + @dbName + '.dbo.sysindexes i '
SET @SQL = @SQL + 'INNER JOIN ' + @dbName + '.dbo.sysobjects o '
SET @SQL = @SQL + 'ON i.id = o.id '
SET @SQL = @SQL + 'WHERE indid > 0 AND indid < 255 AND '
SET @SQL = @SQL + 'o.type = ''U'' AND '
SET @SQL = @SQL + '(i.status & (64 | 8388608)) <= 0'

EXEC sp_executesql @statement = @SQL

SELECT TOP 1 @ID = Indexes_ID, @idxName = IndexName, @objName = ObjectName
FROM ##Indexes
ORDER BY Indexes_ID

SET @RowCnt = @@ROWCOUNT

WHILE @RowCnt <> 0
BEGIN

SET @SQL = 'DBCC DBREINDEX(''' + @dbName + '.dbo.' + @objName + ''', ' + @idxName + ', 0) WITH NO_INFOMSGS'

EXEC sp_executesql @statement = @SQL

SELECT TOP 1 @ID = Indexes_ID, @idxName = IndexName, @objName = ObjectName
FROM ##Indexes
WHERE Indexes_ID > @ID
ORDER BY Indexes_ID

SET @RowCnt = @@ROWCOUNT

END

DROP TABLE ##Indexes

RETURN 0








GO



mk_garg

Kristen
Test

22859 Posts

Posted - 2005-12-08 : 01:08:27
"Do i really need ##indexes, i guess i can change to #indexes?"

Yes you do, and no you can't replace it with #indexes otherwise a) Tara would have done it and b) it won't be in scope

You could create a ## table with a "random" name, and use that.

Your problem is that either the previous execution did NOT complete and drop the table, and that session is still active, or two processes are trying to run the Sproc at the same time - which you need to code against if you are going to put the SProc in MASTER

Personally I would avoid "polluting" MASTER, especially as this Sproc already has a parameter for the database name.

Kristen
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-12-08 : 01:20:47
ok.

If i copy sp in each db and table name is still ##indexes.
Suppose i started reindexing on db A.
Then i started reindexing on db B while reindexing session on db A is still active. will it be successful or not?

Thanks

mk_garg
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-12-08 : 01:40:24
"will it be successful or not"

No. Doesn't matter what DB the Sproc is in. You can execute it from anywhere with:

EXEC MyAdminDBName.dbo.isp_DBCC_DBREINDEX

so you don't need to put it in each DB

Kristen
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-12-08 : 16:35:42
so only alternate is replaace ##indexes with some random name.
How i can create this random name table?

Thanks

mk_garg
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-12-08 : 18:43:38
I used the code you posted, and changed the name of the temp table to #Indexes.

It ran just fine, so it should work for you.





CODO ERGO SUM
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-12-08 : 19:03:27
quote:
Originally posted by Kristen

"Do i really need ##indexes, i guess i can change to #indexes?"

b) it won't be in scope

Kristen



Why it will be out of scope?
I feel Michael is right #indexes should work.


Thanks

mk_garg
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-12-08 : 19:20:00
Why don't you just test this yourself, instead of posting more questions?

It took me about 5 minutes to test it.


quote:
Originally posted by mk_garg20
...Why it will be out of scope?
I feel Michael is right #indexes should work...



CODO ERGO SUM
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-12-08 : 21:11:17
Thanks

mk_garg
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-12-09 : 01:44:12
Well I'll be ... handy to know that though, thanks MVJ

Kristen
Go to Top of Page

bigpa
Starting Member

1 Post

Posted - 2007-11-17 : 04:00:03
I use the following to good effect:

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 0)"
Go to Top of Page
   

- Advertisement -