473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8225
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************ *************@r eallyidont.com> wrote in
message news:bk******** ***********@new s.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
7435
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
5279
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 INTO Table ( ID, Cod, CodArt, Q1, DataUscita ) VALUES (pID, pCod, pCod, pQ1, pDataUscita);
5
1939
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" Fields: fNumber Numeric fString VarChar(50)
1
1955
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
8053
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 want to update that change to the database using stored procedure. But i am not getting How to do it,
1
13227
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 have created the procedure and when I execute this its working ok. The stored procedure uses a datefrom and dateTo which I have set up by
4
2247
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 .ActiveConnection = gObjConnect .CommandType = adCmdStoredProc .CommandText = "s_FRCWP_OpenSPInvoiceList" .Parameters.Append .CreateParameter("@DirectoryId", adInteger,adParamInput,,94) .Execute
4
13135
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, ts2_aldkt_app. The middle part of the database name corresponds to the site name e.g aldkm corresponds to site aldkm etc. Each database has one table tblCustomer which is scripted as follows: if exists (select * from dbo.sysobjects where id =...
7
2691
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 perl script. (I could see from the job log of the stored procedure that the stored procedure was successfully completed) my $sql = "sqlplus -s userid/password $jobId"; $rtnCode = system($sql); I had other similar jobs that ran shorter time (e.g....
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.