473,386 Members | 1,736 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,386 software developers and data experts.

How do I select a column from sp_spaceused into a local variable?

I want to look at the size of the current database, so I can create a new
one if it gets too big (we are working around the 2gb MSDE limit for our
customers).

I would like to do something like this:

DECLARE @size INTEGER

execute BLOB0000.dbo.sp_spaceused

and make @size = the database_size column value that sp_spaceused returns.

Any way to do this?

Thanks.

Jul 20 '05 #1
6 21099

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:bk******************@news.demon.co.uk...
I want to look at the size of the current database, so I can create a new
one if it gets too big (we are working around the 2gb MSDE limit for our
customers).

I would like to do something like this:

DECLARE @size INTEGER

execute BLOB0000.dbo.sp_spaceused

and make @size = the database_size column value that sp_spaceused returns.

Any way to do this?

Thanks.


sp_spaceused returns two result sets, so there's no easy way to handle that
in pure SQL. You could handle it on the client side, or alternatively use
sp_helpdb, which only returns one result set if you don't pass a database
name:

create table dbo.DBs (
dbname sysname,
db_size varchar(20),
owner sysname,
dbid int,
created datetime,
status varchar(1000),
compat int
)
go

insert into dbo.DBs
exec sp_helpdb
go

select cast(replace(db_size, 'MB', '') as decimal(10,2))
from dbo.DBs
where dbname = 'BLOB0000'
go

You can also look into the source code of sp_spaceused/sp_helpdb and create
your own proc based on it, or perhaps use SQLDMO to get the database size.

Simon
Jul 20 '05 #2
Robin Tucker (id*************************@reallyidont.com) writes:
I want to look at the size of the current database, so I can create a new
one if it gets too big (we are working around the 2gb MSDE limit for our
customers).

I would like to do something like this:

DECLARE @size INTEGER

execute BLOB0000.dbo.sp_spaceused

and make @size = the database_size column value that sp_spaceused returns.

Any way to do this?


As Simon said, this is a little tricky, since sp_spaceused returns
two result sets. However, This simple SELECT appears to return the
value you are looking for:

select 8192 * sum(reserved) /1024 from sysindexes
where indid in (1, 0, 255)

You may want to run DBCC UPDATESUAGE prior to running the SELECT to
get accurate numbers.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Hello

check this out. Instead of sp_spaceused you could also use sp_helpdb
to get the size of the DB. With the following code you could get the
size of a DB whatever you are looking for.
======================
set nocount on
declare @size varchar(40)
create table #spaceused (name nvarchar(24), db_size nvarchar(13),
owner nvarchar(24), dbid smallint, created char(11), status
varchar(340),
compatibility_level tinyint
)
insert into #spaceused exec sp_helpdb
select @size = db_size from #spaceused where name = 'iserve'
drop table #spaceused
select @size
======================

Regards,
-Manoj Rajshekar
Jul 20 '05 #4
Ok, yes I can see this, but I don't like to mess around with sys tables for
obvious reasons (they change). I guess I don't have much choice anyway.
Thanks

"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn*********************@127.0.0.1...
Robin Tucker (id*************************@reallyidont.com) writes:
I want to look at the size of the current database, so I can create a new one if it gets too big (we are working around the 2gb MSDE limit for our
customers).

I would like to do something like this:

DECLARE @size INTEGER

execute BLOB0000.dbo.sp_spaceused

and make @size = the database_size column value that sp_spaceused returns.
Any way to do this?


As Simon said, this is a little tricky, since sp_spaceused returns
two result sets. However, This simple SELECT appears to return the
value you are looking for:

select 8192 * sum(reserved) /1024 from sysindexes
where indid in (1, 0, 255)

You may want to run DBCC UPDATESUAGE prior to running the SELECT to
get accurate numbers.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #5
Robin Tucker (id*************************@reallyidont.com) writes:
Ok, yes I can see this, but I don't like to mess around with sys tables
for obvious reasons (they change). I guess I don't have much choice
anyway.


There exists a misconception about the system tables. You often see
people say: "don't use the system tables, because Microsoft may change
them".

But things are not really that. The system tables are part of a published
interface, and while it has happened that Microsoft has changed docuemented
functionality without warning, this can happen with any piece in SQL Server,
not just the system tables. However, most often when Microsoft decides
to drop something, they first deprecate it one version of SQL Server,
and in some later version they remove the functionality completely. (Those
changed without warning usually pertained to smaller details.)

What is important to understand about the system tables is that:
o Not all system tables are documented.
o Many columns in documented system tables are described as "reserved"
or "For internal use only".

If you rely on values in undocumented columns or tables, you are walking
a unsafe path.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Manoj Rajshekar (ma**********@yahoo.com) writes:
check this out. Instead of sp_spaceused you could also use sp_helpdb
to get the size of the DB. With the following code you could get the
size of a DB whatever you are looking for.
======================
set nocount on
declare @size varchar(40)
create table #spaceused (name nvarchar(24), db_size nvarchar(13),
owner nvarchar(24), dbid smallint, created char(11), status
varchar(340),
compatibility_level tinyint
)
insert into #spaceused exec sp_helpdb
select @size = db_size from #spaceused where name = 'iserve'
drop table #spaceused
select @size
======================


However, sp_helpdb does not return the same value. sp_helpdb returns
the size of the database file, which includes any unallocated space.
sp_spaceused returns the size of the allocated extents. Which data
you want depends. If you are looking to finding databases you can
shrink, you certainly need the reserved column in sp_spaceused.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #7

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

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
2
by: Michelle | last post by:
Hi all, I have a query that scans huge table consists of 8 or more millions records. The funny thing is that if I use the query with local variable, the query takes more than 1 minutes,...
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
8
by: jinksto | last post by:
Hello, How do I insert a local variable into a select statement when using the Oracle DB connecter in asp.net 2.0 In Code Behind: I set this the string to be the users id: Protected Sub...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
25
by: ramprat | last post by:
Hi All, I'm essentially trying to extract a value from the pre_05_growth_factor column of my traffic table and assign it to a variable to be used later. Does anyone know why the code below...
1
by: cbjones | last post by:
Can I lable the output column resulting from a SELECT query with a variable from one of the tables which are the source of the data? I know I can hard code the lables in the SELECT statement but...
3
by: lenniekuah | last post by:
Hi Good Guy, I am very surprised of the coding generate error message Error 1 Use of unassigned local variable 'strSql' even though the strSQL variable was declared. Not sure what is wrong...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.