/* create a cursor against sysobjects and perform a repetitive operation
   against the current database. Change the operation by setting 
   @procedure, @criteria, and @type */

 DECLARE @procedure varchar(20), 
         @objectname varchar(30), 
         @criteria varchar(20),
	 @type varchar(2),
	 @database varchar(30) 	

 set nocount on
 
 select @procedure = "sp_spaceused" -- do not include trailing space	
 select @criteria = "%"   
 select @type = "U" 
 /* U - table, P - stored procedure, TR - trigger, V - view */

 select @database = (select name from master..sysdatabases 
	             where dbid = (select dbid from master..sysprocesses
	     		           where spid = @@SPID))
 

 DECLARE object_cursor CURSOR
  FOR
  SELECT name FROM sysobjects 
  WHERE type = @type 
  and name like @criteria 
  order by name
OPEN object_cursor
FETCH NEXT FROM object_cursor INTO @objectname
WHILE (@@fetch_status <> -1)
BEGIN
	print @objectname
	EXECUTE (@procedure + " " + @objectname)
	FETCH NEXT FROM object_cursor INTO @objectname
END
print ""
PRINT "All objects processed."
DEALLOCATE object_cursor