Update Statistics

This example comes from the on line documentation for Microsoft SQL Server 6.5.

if exists (select * from sysobjects 
           where id = object_id('dbo.mysp_updatestats') 
           and sysstat & 0xf = 4)
	drop procedure dbo.mysp_updatestats
GO

CREATE PROCEDURE mysp_updatestats AS

declare @table varchar(30)
declare object_cursor cursor for 
   select name from sysobjects
   where type = 'U'
open object_cursor
fetch next from object_cursor into @table
while (@@fetch_status <> -1)
   begin
   if (@@fetch_status <> -2)
      begin 
      print @table
      exec ("update statistics " + @table)
      end
   fetch next from object_cursor into @table
   end
print "Statistics have been updated for all tables."
deallocate object_cursor
GO