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

Find database size in query analyzer

For SQL Server 2000, I'd like to know the way of looking the database and log size with the Query Analizer. sp_spaceused gives only the current usage. I want to find the total size of the database.

Thanks,
Robinson.
0
son_robin
Asked:
son_robin
  • 5
  • 5
  • 2
  • +2
1 Solution
 
Jan FranekCommented:
Try sp_helpdb
0
 
son_robinAuthor Commented:
This gives the current size only. I want the Total Size of the DB and/or Space Available. If you click on the Database properties from Enterprise Manager these details are there. I want to find from query analyzer.
0
 
Jan FranekCommented:
And what about sp_helpdb with database name. It shows me total size of all segments and also free space on each of them. But I'm on Sybase and may be, MS SQL will show you something else.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
SNilssonCommented:

You can get this using T-Sql, I have scripts that will give you DB size, backup size and space left on disc X.

But I cant provide it until I'm back to work on monday.
0
 
son_robinAuthor Commented:
I will wait for monday Nilsson. Thanks a lot
0
 
SjoerdVerweijCommented:
Here's what EM does (amongst a lot of other things)

select [Total size in KB]=sum(convert(float,size)) * (8192.0/1024.0) from dbo.sysfiles

exec sp_spaceused

DBCC SQLPERF(LOGSPACE)

0
 
son_robinAuthor Commented:
Again you are giving me ways to find Used space. I need the total size!!!
0
 
SjoerdVerweijCommented:
What the heck do you think that first select statement gives you?
0
 
SNilssonCommented:

SjoerdVerweij : no need to be rude.

son_robin: please elaborate on what you need

*Total size of log and data files for current database
*Total size of log and data files for all databases
*The above divided by Total size left on disc
*Other
0
 
son_robinAuthor Commented:
Yes.

If you can right click on the database from the enterprise manager (SQL SEerver 2000) and click on properties, it will give size and space available.

SjoerdVerweij, I know your query can return size (current size). But I want to get the space avilable or Total size. Got it??

Thanks SNilsson!

0
 
SjoerdVerweijCommented:
I thought it was a fairly mild response to an inaccurate and mildly accusatory statement. No need to be belligerent.

(Sorry, but I just had the worst lunch in the history of lunches and the indigestion is doing terrible things to my mood).
0
 
SjoerdVerweijCommented:
Okay, what is the difference between current size and total size?
0
 
son_robinAuthor Commented:
Total size is is the size allocated when you create the database. Current size is the size occupied by the database at present. The space available shows how much size is left so that we can dump data into it.

I need to write a code in the front end to verify if the dtatabse is nearing its available total size and if it does then alert the system admin

Hope this clarification helps.

Thanks,
Robinson.
0
 
SjoerdVerweijCommented:
I see. One more question though: why not set the database to grow automatically?
0
 
arbertCommented:
Agree with SjoerdVerweij, you want to preallocate your database giving enough room to grow, but why not take advantage of automatic growth--some people think it's sloppy, but I think it's better than a suspect database.

Here is a copy of the script we use to get size--it's overkill for your situation, but it does breakdown to the filegroup level:


USE master
GO
IF OBJECTPROPERTY(OBJECT_ID('dbo.sp_filegroup_info'), 'IsProcedure') = 1
BEGIN
    DROP PROCEDURE dbo.sp_filegroup_info
    IF OBJECTPROPERTY(OBJECT_ID('dbo.sp_filegroup_info'), 'IsProcedure') = 1
        PRINT '<<< FAILED DROPPING PROCEDURE dbo.sp_filegroup_info >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE dbo.sp_filegroup_info >>>'
END
go
CREATE PROCEDURE sp_filegroup_info
--WITH ENCRYPTION
AS

CREATE TABLE #FileDetails (
        FileId int , FileGroupId int , TotalExtents int , UsedExtents int ,
        "Name" nvarchar( 128 )  , "FileName" nvarchar( 500 ) ,
        TotalSize AS ( ( TotalExtents * 64.0 ) / 1024 ) ,
        UsedSize AS ( ( UsedExtents * 64.0 ) / 1024 )
)
CREATE TABLE #LogDetails (
        DatabaseName nvarchar( 128 ) , FileSize float ,
        "%Usage" float , Status int
)

-- Get data file info:
INSERT INTO #FileDetails (
        FileId , FileGroupId , TotalExtents , UsedExtents , "Name" , "Filename"
)
EXECUTE( 'dbcc showfilestats with tableresults' )

-- Get log files info:
INSERT INTO #LogDetails (
        DatabaseName , FileSize ,  "%Usage" , Status
)
EXECUTE( 'dbcc sqlperf( logspace ) with tableresults' )

SELECT FILEGROUP_NAME( FileGroupID )  AS FileGroupName , FileId , "Name" , "FileName" ,
       STR( TotalSize , 28 , 2 ) + ' MB' AS FileSize ,
       STR( UsedSize , 28 , 2 ) + ' MB' AS CurrentSize ,
       STR( ( ( UsedExtents * 1. ) / TotalExtents ) * 100 , 5 , 2 ) AS "%Usage"
FROM #FileDetails
UNION all
SELECT '<All Data Filegroups>' , NULL , NULL , NULL ,
       STR( SUM( TotalSize ) , 28 , 2 ) + ' MB' AS FileSize ,
       STR( SUM( UsedSize ) , 28 , 2 ) + ' MB' AS CurrentSize ,
       STR( ( ( SUM( UsedExtents ) * 1. ) / SUM( TotalExtents ) ) * 100 , 5, 2 ) AS "%Usage"
FROM #FileDetails
UNION ALL
SELECT '<Transaction Log>' , NULL , NULL , NULL ,
       STR( FileSize , 28 , 2 ) + ' MB' AS FileSize ,
       NULL ,
       STR( "%Usage" , 5 , 2 ) AS "%Usage"
FROM #LogDetails
WHERE DatabaseName = DB_NAME()
ORDER BY FileGroupName , FileId
go
IF OBJECTPROPERTY(OBJECT_ID('dbo.sp_filegroup_info'), 'IsProcedure') = 1
BEGIN
     GRANT EXECUTE ON dbo.sp_filegroup_info To Public
     PRINT '<<< CREATED PROCEDURE dbo.sp_filegroup_info >>>'
END
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE dbo.sp_filegroup_info >>>'
go
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now