May 18, 2005 at 8:49 am
HI.. I have 2 questions...
I would like to know how i can get all the sizes of all the tables in a database
and also how can i get a listing of all the recovery models for all databases.
Juanita
May 18, 2005 at 8:58 am
You can use the stored procedure sp_spaceused to obtain the size information for each table. You will need to supply the table name as a parameter, so if you want all tables in a database I would suggest using a cursor. For the recovery modes you can execute the following command on each database:
SELECT DATABASEPROPERTYEX(N'Your DB Name', N'RECOVERY')
Again, if you need to obtain this information for an entire server you could use a cursor as well.
May 18, 2005 at 9:08 am
HI.. What exaclty is a cursor? I've never worked with that before?
Juanita
May 18, 2005 at 9:18 am
Here is an example that will generate the sp_spaceused statements for every user table in a database.
DECLARE @l_table_name VARCHAR(255)
DECLARE CHECKSIZE CURSOR For
SELECT name FROM sysobjects WHERE xtype LIKE 'U'
OPEN CHECKSIZE
FETCH NEXT FROM CHECKSIZE INTO @l_table_name
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'EXECUTE sp_spaceused '+''''+@l_table_name+''''
FETCH NEXT FROM CHECKSIZE INTO @l_table_name
END
CLOSE CHECKSIZE
DEALLOCATE CHECKSIZE
Once you run this command you can copy the results to a new query analyzer window and there you have all your sp_spaceused commands. It's a fast way to generate a repetiive list without having to manually type in all the table names.
May 18, 2005 at 9:28 am
Or even faster for the same result without a cursor :
SELECT 'EXECUTE sp_spaceused ''' + name + '''' FROM sysobjects WHERE xtype LIKE 'U' ORDER BY name
The advantage of the cursor in this case is that you can execute the statement instead of only printing it which can help created un-maned jobs.
May 18, 2005 at 10:33 am
Thank you so much !!! These ideas are perfect !!!
Juanita
May 19, 2005 at 11:30 am
Or:
EXECUTE sp_MSforeachtable
@command1=" Print '?' EXECUTE sp_spaceused '?'"
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply