?
Solved

To run a SP against many databases in the same server.

Posted on 2006-06-09
7
Medium Priority
?
419 Views
Last Modified: 2007-12-19
In SQL2K I am trying

sp_Msforeachdb @command1 =
'use [?]
if exists (select 1 from information_schema.tables where table_name = ''Db_true'')
exec sp__defrag_n_reindex '

[A cursor with the name 'hCForEach' already exists.]

I have traced the problematic line and that is the last line in my proc. The full proc is given in my first comment


What is the solution.


0
Comment
Question by:Mateen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:Mateen
ID: 16875628
sp__defrag_n_reindex  is created in master database and has marked as systemobject.

create proc sp__defrag_n_reindex as
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr   VARCHAR (255)
DECLARE @objectid  INT
DECLARE @indexid   INT
DECLARE @frag      DECIMAL
DECLARE @maxfrag   DECIMAL

-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 15.0

-- Declare cursor
DECLARE tables CURSOR FOR
   SELECT TABLE_NAME
   FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_TYPE = 'BASE TABLE'

-- Create the table
CREATE TABLE #fraglist (
   ObjectName CHAR (255),
   ObjectId INT,
   IndexName CHAR (255),
   IndexId INT,
   Lvl INT,
   CountPages INT,
   CountRows INT,
   MinRecSize INT,
   MaxRecSize INT,
   AvgRecSize INT,
   ForRecCount INT,
   Extents INT,
   ExtentSwitches INT,
   AvgFreeBytes INT,
   AvgPageDensity INT,
   ScanDensity DECIMAL,
   BestCount INT,
   ActualCount INT,
   LogicalFrag DECIMAL,
   ExtentFrag DECIMAL)

-- Open the cursor
OPEN tables

-- Loop through all the tables in the database
FETCH NEXT
   FROM tables
   INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
-- Do the showcontig of all indexes of the table
   INSERT INTO #fraglist
   EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''')
      WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')
   FETCH NEXT
      FROM tables
      INTO @tablename
END

-- Close and deallocate the cursor
CLOSE tables
DEALLOCATE tables

-- Declare cursor for list of indexes to be defragged
DECLARE indexes CURSOR FOR
   SELECT ObjectName, ObjectId, IndexId, LogicalFrag
   FROM #fraglist
   WHERE LogicalFrag >= @maxfrag
      AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0

-- Open the cursor
OPEN indexes

-- loop through the indexes
FETCH NEXT
   FROM indexes
   INTO @tablename, @objectid, @indexid, @frag

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ',
      ' + RTRIM(@indexid) + ') - fragmentation currently '
       + RTRIM(CONVERT(varchar(15),@frag)) + '%'
   SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ',
       ' + RTRIM(@indexid) + ')'
   EXEC (@execstr)

   FETCH NEXT
      FROM indexes
      INTO @tablename, @objectid, @indexid, @frag
END

-- Close and deallocate the cursor
CLOSE indexes
DEALLOCATE indexes

-- Delete the temporary table
DROP TABLE #fraglist

exec sp_MSForEachTable 'DBCC DBREINDEX ( ''?'' , '''' , 0 )'


GO
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 16875656
This error is because sp_MSForEachTable nd sp_MSForEachDB uses the same cursor Name
0
 

Author Comment

by:Mateen
ID: 16875664
Ok.
What is the solution to replace
exec sp_MSForEachTable 'DBCC DBREINDEX ( ''?'' , '''' , 0 )'



 
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16875679
i am not sure on this ,I think it is better to run the above statement against all the dbs  without using sp_MsForEachDB
0
 

Author Comment

by:Mateen
ID: 16875680
Hi aneeshattingal

It seems that I have got it right.

Please wait.
0
 

Author Comment

by:Mateen
ID: 16875687
I have already store procedure created in my master database

CREATE procedure sp__ReindexEachTable as
--exec sp_MSForEachTable 'DBCC DBREINDEX ( ''?'' , '''' , 0 )'
declare @TableName     sysname
declare c_Tables cursor for
select Table_Name
from information_schema.tables
where Table_Type = 'Base Table'
open c_Tables
fetch next from c_Tables into @TableName
while @@fetch_status = 0 begin
     print @TableName
     dbcc dbreindex( @TableName, '', 90 )
     execute( N' Update Statistics ' + @TableName )
     fetch next from c_Tables into @TableName
end
close c_Tables
deallocate c_Tables
GO

I deleted the line
exec sp_MSForEachTable 'DBCC DBREINDEX ( ''?'' , '''' , 0 )'
from the PROCEDURE AND replaced

sp_Msforeachdb @command1 =
'use [?]
if exists (select 1 from information_schema.tables where table_name = ''Db_true'')
exec sp__defrag_n_reindex '

                    BY
sp_Msforeachdb @command1 =
'use [?]
if exists (select 1 from information_schema.tables where table_name = ''Db_true'')
exec sp__defrag_n_reindex ',
@command2 = ' use [?] exec sp__ReindexEachTable'

It seems to be working.





0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16875694
ok. that's good..
I was trying to edit one of the sps and change the cursornames ..
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question