[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] 24x7 Script Archive


Automating SQL-Server DBA functions

The following SQL script uses UPDATE STATISTICS for all tables that contain indexes.

Consider scheduling a daily database type job that will execute this SQL

/* declare variables */
declare @table_name varchar(30)

/* declare a cursor that will contain a list of table */
/* names to be updated */
declare idx_cursor cursor
for select distinct a.name
from sysobjects a, sysindexes b
where a.type = `U'
and a.id = b.id
and b.indid > 0

/* open the cursor */
open idx_cursor

/* get the first row from the cursor */
fetch next from idx_cursor into @table_name

/* loop through the rows in the cursor */
while @@fetch_status = 0
begin
   /* issue UPDATE STATISTICS */
   EXEC ("UPDATE STATISTICS " + @table_name)

   /* get next table name */
   fetch next from idx_cursor into @table_name
end