473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Getting database size: a stored procedure to do it......


As I had real problems working my head around sp_spaceused, I've written an
SP to do it (I also noted a lot of questions about this when "searching").
Pass in a database name and it will return the size of the database as a
float (ie. 0.75 for 0.75mb). Update usage set to 1 indicates the DB should
update its size information before giving you the result.

Anyway, comments appreciate.
ALTER PROCEDURE dbo.proc_BL_Get_Space_Used

@DatabaseName NVARCHAR(54),
@updateusage BIT,
@Size REAL OUTPUT
AS
BEGIN
DECLARE @dbsize DECIMAL(15,0)
DECLARE @bytesperpage DECIMAL(15,0)
DECLARE @pagesperMB DECIMAL(15,0)
DECLARE @Error INTEGER
DECLARE @ExecString NVARCHAR(256)
DECLARE @ParmString NVARCHAR(128)

SET @Error = 0

/*
Update usage for this database.
*/

IF @updateusage = 1
BEGIN
DBCC UPDATEUSAGE (@DatabaseName) WITH NO_INFOMSGS
SET @Error = @@ERROR
END

SET NOCOUNT ON

/*
Work out the database size.
*/

IF @Error = 0
BEGIN
SET @ExecString = 'SELECT @dbsize = SUM ( CONVERT ( DECIMAL (
15 ), SIZE ) ) FROM ' + @DatabaseName + '.dbo.sysfiles WHERE (status & 64 =
0)'
SET @ParmString = '@dbsize DECIMAL(15,0) OUTPUT'
EXECUTE sp_executesql @ExecString, @ParmString, @dbsize OUTPUT
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR )
END

/*
and bytes per page.
*/

IF @Error = 0
BEGIN
SELECT @bytesperpage = LOW
FROM master.dbo.spt_values
WHERE number = 1
AND type = 'E'
SET @Error = @@ERROR
END

/*
pages per mb
*/

IF @Error = 0
BEGIN
SELECT @pagesperMB = 1048576 / @bytesperpage
SET @Error = @@ERROR
END

/*
and finally, the result.
*/

IF @Error = 0
BEGIN
SET @Size = CONVERT(REAL, @dbsize) / CONVERT(REAL, @pagesperMB )
SET @Error = @@ERROR
END

RETURN @Error
END
Jul 20 '05 #1
1 8204
Oh, this just works out the db size, it doesn't throw in the log file size
or anything extra like that. I wrote this to check on db sizes for MSDE
(desktop engine), which sets a limit of 2gb. Something we had to work
around in our software.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:bk*******************@news.demon.co.uk...

As I had real problems working my head around sp_spaceused, I've written an SP to do it (I also noted a lot of questions about this when "searching").
Pass in a database name and it will return the size of the database as a
float (ie. 0.75 for 0.75mb). Update usage set to 1 indicates the DB should update its size information before giving you the result.

Anyway, comments appreciate.
ALTER PROCEDURE dbo.proc_BL_Get_Space_Used

@DatabaseName NVARCHAR(54),
@updateusage BIT,
@Size REAL OUTPUT
AS
BEGIN
DECLARE @dbsize DECIMAL(15,0)
DECLARE @bytesperpage DECIMAL(15,0)
DECLARE @pagesperMB DECIMAL(15,0)
DECLARE @Error INTEGER
DECLARE @ExecString NVARCHAR(256)
DECLARE @ParmString NVARCHAR(128)

SET @Error = 0

/*
Update usage for this database.
*/

IF @updateusage = 1
BEGIN
DBCC UPDATEUSAGE (@DatabaseName) WITH NO_INFOMSGS
SET @Error = @@ERROR
END

SET NOCOUNT ON

/*
Work out the database size.
*/

IF @Error = 0
BEGIN
SET @ExecString = 'SELECT @dbsize = SUM ( CONVERT ( DECIMAL (
15 ), SIZE ) ) FROM ' + @DatabaseName + '.dbo.sysfiles WHERE (status & 64 = 0)'
SET @ParmString = '@dbsize DECIMAL(15,0) OUTPUT'
EXECUTE sp_executesql @ExecString, @ParmString, @dbsize OUTPUT
SET @Error = COALESCE ( NULLIF ( @Error, 0 ), @@ERROR )
END

/*
and bytes per page.
*/

IF @Error = 0
BEGIN
SELECT @bytesperpage = LOW
FROM master.dbo.spt_values
WHERE number = 1
AND type = 'E'
SET @Error = @@ERROR
END

/*
pages per mb
*/

IF @Error = 0
BEGIN
SELECT @pagesperMB = 1048576 / @bytesperpage
SET @Error = @@ERROR
END

/*
and finally, the result.
*/

IF @Error = 0
BEGIN
SET @Size = CONVERT(REAL, @dbsize) / CONVERT(REAL, @pagesperMB ) SET @Error = @@ERROR
END

RETURN @Error
END

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rkershberg | last post by:
Is there a stored procedure or extended stored proc which returns the table row size defined for a table ? Thanks, Rob
9
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT...
5
by: joun | last post by:
As suggested by Cor Ligthert, i've created a simpler sample, with the same problem; this is the full source code, so everyone can try itself: Access database "dati.mdb": Tables: "myTable"...
1
by: A.M. de Jong | last post by:
Hi, What is the syntax of storing the content of an Excelsheet in a predefined table with a stored prcedure. Arno de Jong, The Netherlands.
2
by: chandu | last post by:
Hi Guys, i am beginer in the worls of Asp.net and C#.net I have one problem that I have stored my changes of data into dataset using disconnected dataset and after completion of all changes i...
1
by: apothecary | last post by:
Hello Newbie here. Is there a way of creating a VIEW...using a stored procedure. I am basically trying to create a view to return some data that I am getting using a stored procedure. I...
4
rrocket
by: rrocket | last post by:
I currently have this that sends a value to the DB and should return the information I need: Set RS = createobject("ADODB.Connection") Set x1 = server.CreateObject("ADODB.Command") With x1...
4
by: gamaz | last post by:
Hi, I am trying to work on a stored procedure that will work with multiple database. I have a prototype of multiple databases. Those are named as the following: ts2_aldkm_app, ts2_aldkp_app,...
7
by: sammak168 | last post by:
I had a Perl script that called a sql file calling the stored procedure in Oracle database. The stored procedure ran for a long time (more than 1.5 hrs) and did not return the result code to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.