• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

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

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
Mateen
Asked:
Mateen
  • 4
  • 3
1 Solution
 
MateenAuthor Commented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
This error is because sp_MSForEachTable nd sp_MSForEachDB uses the same cursor Name
0
 
MateenAuthor Commented:
Ok.
What is the solution to replace
exec sp_MSForEachTable 'DBCC DBREINDEX ( ''?'' , '''' , 0 )'



 
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
MateenAuthor Commented:
Hi aneeshattingal

It seems that I have got it right.

Please wait.
0
 
MateenAuthor Commented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
ok. that's good..
I was trying to edit one of the sps and change the cursornames ..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now