Showing posts with label space. Show all posts
Showing posts with label space. Show all posts

Thursday, March 29, 2012

Database in recovery

Our server ran out of disk space during a stored procedure process. We were able to free up some space, stopped all SQL services, restarted all SQL services and now SQL Management Studio shows the database to be in recovery. Is there a way to monitor this to ensure progress is being made and to estimate when it might complete?

I don't know it can be checked programatically but you can sql error log where you recovery progress will be recorded.

Looks like you guys shutdown the servers in the middle of big transaction....

There will no other option than waiting to recover...

|||

There is a way, via T-SQL to view process progress but apparently not via GUI. The database did eventually recover and we have submitted for a new server as it ran out of disk space during a stored procedure run.

Thanks!

Tuesday, March 27, 2012

Database Growth & Space Recovery Problem

I have done this experiment on one of the tables. There is table called
build havinf nvText Field with large no of records. I want to drop that
column and recover space. These are the results I got.
SP_SPACEUSED BUILD Results
name rows reserved data index_size unused
1. Before Deleting nvText Field
Build 663211 341440 KB 339464 KB 1944 KB 32 KB
2. After Deleting nvText Field
Build 663211 341440 KB 339464 KB 1944 KB 32 KB
3. After Executing the Shrink Database from Enterprise Manager.
Build 663211 608280 KB 604744 KB 3456 KB 80 K
4. After Executing DBCC DBReindex (build,'',70)
Build 663211 124096 KB 123392 KB 712 KB -8 KB
Can anyone please explain me after executing step 3 i.e shrink data
column as well as index_size shows an increased figure whereas logically
it should be a reduced figure.
Regards,
Harcharan
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!
One thing is that you should be suspicious of the numbers , as you are..
SP_SPaceused reports information from Sysindexes, but the sysindexes info is
NOT updated with everyinsert update or delete...So the numbers may not
reflect the actual size..
For each sp_spaceused in your test , execute like this
exec sp_spaceused 'build', true
The second parameter tells sql to read through the table and update the
estimates, it runs longer but you will get tru results...If the numbers
still look funny, we'll try something else...
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Harcharan Jassal" <hjjassal@.yahoo.com> wrote in message
news:uCtYWSBhEHA.3540@.TK2MSFTNGP10.phx.gbl...
> I have done this experiment on one of the tables. There is table called
> build havinf nvText Field with large no of records. I want to drop that
> column and recover space. These are the results I got.
> SP_SPACEUSED 'BUILD' Results
> name rows reserved data index_size unused
> 1. Before Deleting nvText Field
> Build 663211 341440 KB 339464 KB 1944 KB 32 KB
> 2. After Deleting nvText Field
> Build 663211 341440 KB 339464 KB 1944 KB 32 KB
> 3. After Executing the Shrink Database from Enterprise Manager.
> Build 663211 608280 KB 604744 KB 3456 KB 80 K
> 4. After Executing DBCC DBReindex (build,'',70)
> Build 663211 124096 KB 123392 KB 712 KB -8 KB
> Can anyone please explain me after executing step 3 i.e shrink data
> column as well as index_size shows an increased figure whereas logically
> it should be a reduced figure.
> Regards,
> Harcharan
>
> *** Sent via Developersdex http://www.codecomments.com ***
> Don't just participate in USENET...get rewarded for it!

Database Growth & Space Recovery Problem

I have done this experiment on one of the tables. There is table called
build havinf nvText Field with large no of records. I want to drop that
column and recover space. These are the results I got.
SP_SPACEUSED BUILD Results
name rows reserved data index_size unused
1. Before Deleting nvText Field
Build 663211 341440 KB 339464 KB 1944 KB 32 KB
2. After Deleting nvText Field
Build 663211 341440 KB 339464 KB 1944 KB 32 KB
3. After Executing the Shrink Database from Enterprise Manager.
Build 663211 608280 KB 604744 KB 3456 KB 80 K
4. After Executing DBCC DBReindex (build,'',70)
Build 663211 124096 KB 123392 KB 712 KB -8 KB
Can anyone please explain me after executing step 3 i.e shrink data
column as well as index_size shows an increased figure whereas logically
it should be a reduced figure.
Regards,
Harcharan
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!One thing is that you should be suspicious of the numbers , as you are..
SP_SPaceused reports information from Sysindexes, but the sysindexes info is
NOT updated with everyinsert update or delete...So the numbers may not
reflect the actual size..
For each sp_spaceused in your test , execute like this
exec sp_spaceused 'build', true
The second parameter tells sql to read through the table and update the
estimates, it runs longer but you will get tru results...If the numbers
still look funny, we'll try something else...
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Harcharan Jassal" <hjjassal@.yahoo.com> wrote in message
news:uCtYWSBhEHA.3540@.TK2MSFTNGP10.phx.gbl...
> I have done this experiment on one of the tables. There is table called
> build havinf nvText Field with large no of records. I want to drop that
> column and recover space. These are the results I got.
> SP_SPACEUSED 'BUILD' Results
> name rows reserved data index_size unused
> 1. Before Deleting nvText Field
> Build 663211 341440 KB 339464 KB 1944 KB 32 KB
> 2. After Deleting nvText Field
> Build 663211 341440 KB 339464 KB 1944 KB 32 KB
> 3. After Executing the Shrink Database from Enterprise Manager.
> Build 663211 608280 KB 604744 KB 3456 KB 80 K
> 4. After Executing DBCC DBReindex (build,'',70)
> Build 663211 124096 KB 123392 KB 712 KB -8 KB
> Can anyone please explain me after executing step 3 i.e shrink data
> column as well as index_size shows an increased figure whereas logically
> it should be a reduced figure.
> Regards,
> Harcharan
>
> *** Sent via Developersdex http://www.codecomments.com ***
> Don't just participate in USENET...get rewarded for it!sql

Sunday, March 25, 2012

Database free space: big discrepancy between sp_spaceused and EM taskpad view

I am monitoring free space in my database so I can manually grow the
data file rather than relying on autogrow. I get the free space by
using the 'unallocated space' value returned by sp_spaceused.
Today I was alerted that there was less than 2% free space remaining
in my database. I verified with sp_spaceused and it was reporting ~8GB
unallocated space in a ~500GB database. Then I checked the free space
by looking at the taskpad view for my database in Enterprise Manager.
This showed ~60GB free. I ran a profiler trace to see what EM is doing
to calculate free space but I can't tell what calculations it does,
other than to see that in addition to sp_spaceused it uses the
following two commands:
select sum(convert(float,size)) * (8192.0/1024.0) from dbo.sysfiles
and
DBCC showfilestats
Am I wrong in thinking that the 'unallocated space' and free space
reported by EM should be similar? Is one a more reliable indicator
than the other?
ThanksHi
Before using sp_spaceused run DBCC UPDATEUSAGE command
<pshroads@.gmail.com> wrote in message
news:1184779707.553225.56190@.d30g2000prg.googlegroups.com...
>I am monitoring free space in my database so I can manually grow the
> data file rather than relying on autogrow. I get the free space by
> using the 'unallocated space' value returned by sp_spaceused.
> Today I was alerted that there was less than 2% free space remaining
> in my database. I verified with sp_spaceused and it was reporting ~8GB
> unallocated space in a ~500GB database. Then I checked the free space
> by looking at the taskpad view for my database in Enterprise Manager.
> This showed ~60GB free. I ran a profiler trace to see what EM is doing
> to calculate free space but I can't tell what calculations it does,
> other than to see that in addition to sp_spaceused it uses the
> following two commands:
> select sum(convert(float,size)) * (8192.0/1024.0) from dbo.sysfiles
> and
> DBCC showfilestats
> Am I wrong in thinking that the 'unallocated space' and free space
> reported by EM should be similar? Is one a more reliable indicator
> than the other?
> Thanks
>

Database free space: big discrepancy between sp_spaceused and EM taskpad view

I am monitoring free space in my database so I can manually grow the
data file rather than relying on autogrow. I get the free space by
using the 'unallocated space' value returned by sp_spaceused.
Today I was alerted that there was less than 2% free space remaining
in my database. I verified with sp_spaceused and it was reporting ~8GB
unallocated space in a ~500GB database. Then I checked the free space
by looking at the taskpad view for my database in Enterprise Manager.
This showed ~60GB free. I ran a profiler trace to see what EM is doing
to calculate free space but I can't tell what calculations it does,
other than to see that in addition to sp_spaceused it uses the
following two commands:
select sum(convert(float,size)) * (8192.0/1024.0) from dbo.sysfiles
and
DBCC showfilestats
Am I wrong in thinking that the 'unallocated space' and free space
reported by EM should be similar? Is one a more reliable indicator
than the other?
ThanksHi
Before using sp_spaceused run DBCC UPDATEUSAGE command
<pshroads@.gmail.com> wrote in message
news:1184779707.553225.56190@.d30g2000prg.googlegroups.com...
>I am monitoring free space in my database so I can manually grow the
> data file rather than relying on autogrow. I get the free space by
> using the 'unallocated space' value returned by sp_spaceused.
> Today I was alerted that there was less than 2% free space remaining
> in my database. I verified with sp_spaceused and it was reporting ~8GB
> unallocated space in a ~500GB database. Then I checked the free space
> by looking at the taskpad view for my database in Enterprise Manager.
> This showed ~60GB free. I ran a profiler trace to see what EM is doing
> to calculate free space but I can't tell what calculations it does,
> other than to see that in addition to sp_spaceused it uses the
> following two commands:
> select sum(convert(float,size)) * (8192.0/1024.0) from dbo.sysfiles
> and
> DBCC showfilestats
> Am I wrong in thinking that the 'unallocated space' and free space
> reported by EM should be similar? Is one a more reliable indicator
> than the other?
> Thanks
>

Database free space vs. Table unused space

Hi,
I've had a query from an analyst regarding database free space and table
unused space. We have a database made up of 3 filegroups; with total sizes
of 100GB, 25GB, & 27GB , and free space is reported as 65MB, 70MB, & 2MB
respectively.
Now, if he executes "sp_spaceused" against one specific table that he's
working with (trying to reduce size by improving purging routines, etc), the
n
it reports the data and index sizes, but also reports over 2GB of "unused
space". This is after running an updateusage, etc.
What is this referring to? Can it be freed to the OS? Why does it not show
in the database free space (as obviously 2GB unused space can't fit in this
database's free space)? Any light shed on this would be appreciated.
Thanks,
daveThe unused value reported by sp_spaceused are pages on extents that are owne
d by that particular
objects, but the pages are currently not in use. So the pages can be used by
the object that owns
the extent in question but not by other extents.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DavidCur" <cdf_nospam_@.webmail.co.za> wrote in message
news:2F6AA526-E215-4EC7-AEBA-7C454F937FC4@.microsoft.com...
> Hi,
> I've had a query from an analyst regarding database free space and table
> unused space. We have a database made up of 3 filegroups; with total size
s
> of 100GB, 25GB, & 27GB , and free space is reported as 65MB, 70MB, & 2MB
> respectively.
> Now, if he executes "sp_spaceused" against one specific table that he's
> working with (trying to reduce size by improving purging routines, etc), t
hen
> it reports the data and index sizes, but also reports over 2GB of "unused
> space". This is after running an updateusage, etc.
> What is this referring to? Can it be freed to the OS? Why does it not sh
ow
> in the database free space (as obviously 2GB unused space can't fit in thi
s
> database's free space)? Any light shed on this would be appreciated.
> Thanks,
> dave|||"Tibor Karaszi" wrote:

> The unused value reported by sp_spaceused are pages on extents that are ow
ned by that particular
> objects, but the pages are currently not in use. So the pages can be used
by the object that owns
> the extent in question but not by other extents.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
Yes, I understand that - but I wanted to know if there was a way to free
that unused space up? What governs how much unused space a table has? Has
it got something to do with the FILLFACTOR of indexes? I'm just trying to
understand how this table can have so much (>2GB) unused space.
Thanks,
dave|||Higher fillfactor will mean less pages. But I don't think that less fuller p
ages will add to the
unused value. I believe that only not allocated pages on uniform extents wil
l. Check out the source
code for sp_spaceused (etc.) to be certain. I guess that an index rebuild (o
r reorg) can less the
unused value.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DavidCur" <cdf_nospam_@.webmail.co.za> wrote in message
news:EDD26B2C-1B3C-43AF-911D-2BAD4486C373@.microsoft.com...
> "Tibor Karaszi" wrote:
>
>
> Yes, I understand that - but I wanted to know if there was a way to free
> that unused space up? What governs how much unused space a table has? Ha
s
> it got something to do with the FILLFACTOR of indexes? I'm just trying to
> understand how this table can have so much (>2GB) unused space.
> Thanks,
> dave

Database free space vs. Table unused space

Hi,
I've had a query from an analyst regarding database free space and table
unused space. We have a database made up of 3 filegroups; with total sizes
of 100GB, 25GB, & 27GB , and free space is reported as 65MB, 70MB, & 2MB
respectively.
Now, if he executes "sp_spaceused" against one specific table that he's
working with (trying to reduce size by improving purging routines, etc), then
it reports the data and index sizes, but also reports over 2GB of "unused
space". This is after running an updateusage, etc.
What is this referring to? Can it be freed to the OS? Why does it not show
in the database free space (as obviously 2GB unused space can't fit in this
database's free space)? Any light shed on this would be appreciated.
Thanks,
daveThe unused value reported by sp_spaceused are pages on extents that are owned by that particular
objects, but the pages are currently not in use. So the pages can be used by the object that owns
the extent in question but not by other extents.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DavidCur" <cdf_nospam_@.webmail.co.za> wrote in message
news:2F6AA526-E215-4EC7-AEBA-7C454F937FC4@.microsoft.com...
> Hi,
> I've had a query from an analyst regarding database free space and table
> unused space. We have a database made up of 3 filegroups; with total sizes
> of 100GB, 25GB, & 27GB , and free space is reported as 65MB, 70MB, & 2MB
> respectively.
> Now, if he executes "sp_spaceused" against one specific table that he's
> working with (trying to reduce size by improving purging routines, etc), then
> it reports the data and index sizes, but also reports over 2GB of "unused
> space". This is after running an updateusage, etc.
> What is this referring to? Can it be freed to the OS? Why does it not show
> in the database free space (as obviously 2GB unused space can't fit in this
> database's free space)? Any light shed on this would be appreciated.
> Thanks,
> dave|||"Tibor Karaszi" wrote:
> The unused value reported by sp_spaceused are pages on extents that are owned by that particular
> objects, but the pages are currently not in use. So the pages can be used by the object that owns
> the extent in question but not by other extents.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
Yes, I understand that - but I wanted to know if there was a way to free
that unused space up? What governs how much unused space a table has? Has
it got something to do with the FILLFACTOR of indexes? I'm just trying to
understand how this table can have so much (>2GB) unused space.
Thanks,
dave|||Higher fillfactor will mean less pages. But I don't think that less fuller pages will add to the
unused value. I believe that only not allocated pages on uniform extents will. Check out the source
code for sp_spaceused (etc.) to be certain. I guess that an index rebuild (or reorg) can less the
unused value.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DavidCur" <cdf_nospam_@.webmail.co.za> wrote in message
news:EDD26B2C-1B3C-43AF-911D-2BAD4486C373@.microsoft.com...
> "Tibor Karaszi" wrote:
>> The unused value reported by sp_spaceused are pages on extents that are owned by that particular
>> objects, but the pages are currently not in use. So the pages can be used by the object that owns
>> the extent in question but not by other extents.
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>
> Yes, I understand that - but I wanted to know if there was a way to free
> that unused space up? What governs how much unused space a table has? Has
> it got something to do with the FILLFACTOR of indexes? I'm just trying to
> understand how this table can have so much (>2GB) unused space.
> Thanks,
> davesql

Database Free Space

Hi,
I have below SQL Qurey which returns the used space for the database,
SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
indid in (0,1,255)
The above query returns the value in MB's or kb's which I am unable to
verify ?
Below is another SQL query which returns the database size in MB's
which I have verified with sp_spaceused.
SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
If the above queries are correct then if I calculate the free space, I
get incorrect results.
FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
master..sysindexes WHERE indid in (0,1,255)))
I have crosschecked the same with sp_spaceused. Is anything wrong here?
Thanks,
Regards,
Pramod
Hi
Have you looked at the source code for sp_spaceused as you seem to be trying
to replicate what it does!
John
"ipramod@.gmail.com" wrote:

> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod
>
|||Hi, Pramond
For normal indexes (between 2 and 254), the reserved column in
sysindexes returns the number of pages allocated for that index. A page
has a size of 8KB. If an index is a clustered index (indid=1), then it
also contains the number of pages allocated for the data in the table.
If a table has no index, there is a row in sysindexes with indid=0,
which returns the number of pages allocated for all the data in the
table. For tables that contain text and image data, there is also a row
in sysindexes with indid=255, which returns the number of pages
allocated for text and image data.
In conclusion, you need to consider all indexes, not only the ones with
indid in (0,1,255), so try:
select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
The above query returns the size (in MB) of unallocated pages. You may
have more unused space in the allocated data pages, which is not
returned by the above query.
Razvan
ipramod@.gmail.com wrote:
> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod
|||Hi
Thanks for your feedback.
I think the value returned by the query specified by you, still does
not give the correct result as per sp_spaceused.
Suppose I have a test database for which I execute sp_spaceused which
is as below:
database_name database_size unallocated space
test 1200.00 MB 929.55 MB
reserved data index_size unused
-- -- --
72136 KB 54712 KB 9544 KB 7880 KB
Now if I execute the query suggested by you...
select ((SELECT sum(convert(dec(15),size))/128 FROM test..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM test..sysindexes))
It gives me the unallocated space as 1134.570332 MB which is not
correct as per sp_spaceused (sp_spaceused returned 929.55 MB).
I think the query specified by you does not the exact space used by the
database.
Please give me the correct query to get the unallocated space for a
database.
Thanks,
Regards,
Pramod
Razvan Socol wrote:[vbcol=seagreen]
> Hi, Pramond
> For normal indexes (between 2 and 254), the reserved column in
> sysindexes returns the number of pages allocated for that index. A page
> has a size of 8KB. If an index is a clustered index (indid=1), then it
> also contains the number of pages allocated for the data in the table.
> If a table has no index, there is a row in sysindexes with indid=0,
> which returns the number of pages allocated for all the data in the
> table. For tables that contain text and image data, there is also a row
> in sysindexes with indid=255, which returns the number of pages
> allocated for text and image data.
> In conclusion, you need to consider all indexes, not only the ones with
> indid in (0,1,255), so try:
> select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
> - (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
> The above query returns the size (in MB) of unallocated pages. You may
> have more unused space in the allocated data pages, which is not
> returned by the above query.
> Razvan
> ipramod@.gmail.com wrote:
|||If you want the value returned by sp_spaceused, then use somethink like
this (adapted from the code found in sp_spaceused):
select ltrim(str(((
select sum(convert(dec(15),size))
from dbo.sysfiles
where (status & 64 = 0)
) - (
select sum(convert(dec(15),reserved))
from sysindexes
where indid in (0, 1, 255)
)) / (
select 1048576 / low
from master.dbo.spt_values
where number = 1 and type = 'E'
),15,2)+ ' MB')
Razvan
|||Hi Razvan,
I have sorted out the issue with the help of storedproc sp_spaceused
Below is the query to get the database size, unallocated size and the %
free space and all in MB's
declare @.pagesbigint
,@.dbname sysname
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
then size else 0 end))
from dbo.sysfiles
select @.reservedpages = sum(a.total_pages)
from sys.partitions p join sys.allocation_units a on p.partition_id =
a.container_id
left join sys.internal_tables it on p.object_id = it.object_id
select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
(15,2),@.logsize))/128.00
select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
(15,2),@.reservedpages)) * 8192 / 1048576
select 'DatabaseSize(MB)'=@.totalsize,
'UnallocatedSize(MB)'=@.unallocatedsize,
'DatabaseFreeSpaceInPercent(MB)'=(@.unallocatedsize *1.00/@.totalsize)*100.00
Please let me know if I am wrong.
Thanks,
Regards,
Pramod
Razvan Socol wrote:
> If you want the value returned by sp_spaceused, then use somethink like
> this (adapted from the code found in sp_spaceused):
> select ltrim(str(((
> select sum(convert(dec(15),size))
> from dbo.sysfiles
> where (status & 64 = 0)
> ) - (
> select sum(convert(dec(15),reserved))
> from sysindexes
> where indid in (0, 1, 255)
> )) / (
> select 1048576 / low
> from master.dbo.spt_values
> where number = 1 and type = 'E'
> ),15,2)+ ' MB')
> Razvan
|||Hi Razvan,
Thanks for your feedback.
Now, I have another question.
I have a variable @.dbsize to which I am assigning the value of database
size and I am using the variable value in the code
Below is my SQL query which returns the database free space in percent
for all the databases.
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[dbsize] [varchar] (1000),
[logsize] [varchar] (1000)
)
DELETE FROM #test2
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
--EXEC sp_MSForeachdb 'use [?]; select
db_name();select @.dbsize =
sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
@.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
0 end))FROM ?.dbo.sysfiles'
SELECT @.dbsize = sum(convert(bigint,case when status
& 64 = 0 then
size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM dbo.sysfiles
SELECT @.reservedpages = sum(a.total_pages)
FROM sys.partitions p join sys.allocation_units a on
p.partition_id
= a.container_id
left join sys.internal_tables it on p.object_id =
it.object_id
SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
convert (dec
(15,2),@.logsize))/128.00
SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
- convert
(dec (15,2),@.reservedpages)) * 8192 / 1048576
SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Now this code returns the free space value in percent only for one
database because I am unable to substitute the database name when I
calculate the @.dbsize.
Can you help me?
Thanks,
Regards,
Pramod
ipramod@.gmail.com wrote:[vbcol=seagreen]
> Hi Razvan,
> I have sorted out the issue with the help of storedproc sp_spaceused
> Below is the query to get the database size, unallocated size and the %
> free space and all in MB's
> declare @.pagesbigint
> ,@.dbname sysname
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
> select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
> else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
> then size else 0 end))
> from dbo.sysfiles
> select @.reservedpages = sum(a.total_pages)
> from sys.partitions p join sys.allocation_units a on p.partition_id =
> a.container_id
> left join sys.internal_tables it on p.object_id = it.object_id
> select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
> (15,2),@.logsize))/128.00
> select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
> (15,2),@.reservedpages)) * 8192 / 1048576
> select 'DatabaseSize(MB)'=@.totalsize,
> 'UnallocatedSize(MB)'=@.unallocatedsize,
> 'DatabaseFreeSpaceInPercent(MB)'=(@.unallocatedsize *1.00/@.totalsize)*100.00
>
> Please let me know if I am wrong.
> Thanks,
> Regards,
> Pramod
>
> Razvan Socol wrote:
|||Hi,
I have sorted out the issue by using the temporary tables. I have used
your suggestion and in the 'exec' itself I have inserted the variable
values in the temporary table and it worked. Thanks for your feedback
guys
I am copying the solution here, plz take a look and let me know if I am
wrong and if possible give me another solution. Also, can you tell me
is there any disadvantages of having temp tables in the query?
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
DELETE FROM #test2
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[DatabaseSize] [bigint],
[LogSize] [bigint]
)
DELETE FROM #test3
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize float
,@.totalsize float
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.sql = N'DECLARE @.dbsize1 bigint,@.logsize1
bigint;
SELECT @.dbsize1 = sum(convert(bigint,case when
status & 64 = 0 then
size else 0 end)), @.logsize1 = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM ['+ @.DBname +'].dbo.sysfiles;
INSERT INTO #test3 SELECT @.dbsize1, @.logsize1;'
EXEC sp_executesql @.sql
SELECT @.dbsize=[DatabaseSize], @.logsize=[LogSize]
FROM #test3
SET @.sql = N'DECLARE @.reservedpages1 bigint;
SELECT @.reservedpages1 = sum(a.total_pages)
FROM ['+ @.DBname +'].sys.partitions p join ['+
@.DBname
+'].sys.allocation_units a on p.partition_id = a.container_id
left join ['+ @.DBname +'].sys.internal_tables
it on p.object_id =
it.object_id;
INSERT INTO #test3 SELECT @.reservedpages1, 0;'
EXEC sp_executesql @.sql
SELECT @.reservedpages=[DatabaseSize] FROM #test3
SELECT @.totalsize=(convert (dec (15,2),@.dbsize)*1.00
+ convert (dec
(15,2),@.logsize))*1.00/128.00
SELECT @.unallocatedsize=(convert (dec
(15,2),@.dbsize)*1.00 -
convert (dec (15,2),@.reservedpages)*1.00) * 8192.00 / 1048576.00
SET @.str =
str((@.unallocatedsize*1.00/@.totalsize*1.00)*100.00, 15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Thanks,
Pramod
ipramod@.gmail.com wrote:[vbcol=seagreen]
> Hi Razvan,
> Thanks for your feedback.
> Now, I have another question.
> I have a variable @.dbsize to which I am assigning the value of database
> size and I am using the variable value in the code
> Below is my SQL query which returns the database free space in percent
> for all the databases.
> SET nocount on
> DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
> master..sysdatabases
>
> OPEN AllDatabaseInfo
>
> IF object_id('tempdb..#test2') IS NOT NULL
> BEGIN
> DROP TABLE #test2
> END
>
> CREATE TABLE #test2 (
> [Database Name] [varchar] (1000),
> [Database Space Available] [varchar] (1000)
> )
>
> IF object_id('tempdb..#test3') IS NOT NULL
> BEGIN
> DROP TABLE #test3
> END
>
> CREATE TABLE #test3 (
> [dbsize] [varchar] (1000),
> [logsize] [varchar] (1000)
> )
>
> DELETE FROM #test2
> DECLARE @.DBName nvarchar(1000)
> DECLARE @.sql nvarchar(1000)
> DECLARE @.str sysname
> SET @.sql = ''
> SET @.DBName = ''
> DECLARE @.pages bigint
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
>
> FETCH NEXT FROM AllDatabaseInfo into @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> --
> --EXEC sp_MSForeachdb 'use [?]; select
> db_name();select @.dbsize =
> sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
> @.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
> 0 end))FROM ?.dbo.sysfiles'
>
> SELECT @.dbsize = sum(convert(bigint,case when status
> & 64 = 0 then
> size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
> <> 0 then size else 0 end))
> FROM dbo.sysfiles
>
> SELECT @.reservedpages = sum(a.total_pages)
> FROM sys.partitions p join sys.allocation_units a on
> p.partition_id
> = a.container_id
> left join sys.internal_tables it on p.object_id =
> it.object_id
>
> SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
> convert (dec
> (15,2),@.logsize))/128.00
> SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
> - convert
> (dec (15,2),@.reservedpages)) * 8192 / 1048576
> --
>
> SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
> 15,2)
> SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
> + ''', ' +
> @.str
> EXEC sp_executesql @.sql
> FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
> END
>
> CLOSE AllDatabaseInfo
> DEALLOCATE AllDatabaseInfo
>
> SELECT * FROM #test2
> SET nocount off
>
> Now this code returns the free space value in percent only for one
> database because I am unable to substitute the database name when I
> calculate the @.dbsize.
>
> Can you help me?
>
> Thanks,
> Regards,
> Pramod
>
> ipramod@.gmail.com wrote:
|||For SQL Server 2005, I would use something like this:
select ltrim(str(
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
,15,2)+ ' MB')
from (
select sum(convert(dec(15,2),size)) as dbsize
from dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from sys.allocation_units
) y
If you need the unallocated space for all databases, you can use this:
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#AllDatabaseInfo') IS NOT NULL
DROP TABLE #AllDatabaseInfo
CREATE TABLE #AllDatabaseInfo (
DatabaseName sysname PRIMARY KEY,
UnallocatedSpace decimal(15,2) NOT NULL
)
DECLARE AllDatabaseInfo CURSOR LOCAL FOR
SELECT name FROM master..sysdatabases
OPEN AllDatabaseInfo
DECLARE @.DBName sysname, @.SQL nvarchar(4000)
WHILE 1=1 BEGIN
FETCH NEXT FROM AllDatabaseInfo into @.DBName
IF @.@.FETCH_STATUS<>0 BREAK
SET @.SQL = N'insert into #AllDatabaseInfo
select '+QUOTENAME(@.DBName,'''')+',
convert(dec(15,2),
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
)
from (
select sum(convert(dec(15,2),size)) as dbsize
from '+QUOTENAME(@.DBName)+'.dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from '+QUOTENAME(@.DBName)+'.sys.allocation_units
) y
'
--PRINT @.SQL
EXEC (@.SQL)
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SET NOCOUNT OFF
SELECT * FROM #AllDatabaseInfo ORDER BY DatabaseName
Razvan

Database free space

I noticed a database was showing 0 mb of free space even though I see 7 gb of
free space when I go to shrink. I added another 10 gb and I still see 0 mb of
free space. Is this a bug?
Try executing DBCC UPDATEUSAGE - you can find more
information on the command in books online.
Then check the space after you have executed the command.
-Sue
On Tue, 9 Nov 2004 13:19:07 -0800, "Derek"
<evans561@.hotmail.com> wrote:

>I noticed a database was showing 0 mb of free space even though I see 7 gb of
>free space when I go to shrink. I added another 10 gb and I still see 0 mb of
>free space. Is this a bug?

Database Free Space

Hi,
I have below SQL Qurey which returns the used space for the database,
SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
indid in (0,1,255)
The above query returns the value in MB's or kb's which I am unable to
verify ?
Below is another SQL query which returns the database size in MB's
which I have verified with sp_spaceused.
SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
If the above queries are correct then if I calculate the free space, I
get incorrect results.
FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
master..sysindexes WHERE indid in (0,1,255)))
I have crosschecked the same with sp_spaceused. Is anything wrong here?
Thanks,
Regards,
PramodHi
Have you looked at the source code for sp_spaceused as you seem to be trying
to replicate what it does!
John
"ipramod@.gmail.com" wrote:
> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod
>|||Hi, Pramond
For normal indexes (between 2 and 254), the reserved column in
sysindexes returns the number of pages allocated for that index. A page
has a size of 8KB. If an index is a clustered index (indid=1), then it
also contains the number of pages allocated for the data in the table.
If a table has no index, there is a row in sysindexes with indid=0,
which returns the number of pages allocated for all the data in the
table. For tables that contain text and image data, there is also a row
in sysindexes with indid=255, which returns the number of pages
allocated for text and image data.
In conclusion, you need to consider all indexes, not only the ones with
indid in (0,1,255), so try:
select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
The above query returns the size (in MB) of unallocated pages. You may
have more unused space in the allocated data pages, which is not
returned by the above query.
Razvan
ipramod@.gmail.com wrote:
> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod|||Hi
Thanks for your feedback.
I think the value returned by the query specified by you, still does
not give the correct result as per sp_spaceused.
Suppose I have a test database for which I execute sp_spaceused which
is as below:
database_name database_size unallocated space
----
test 1200.00 MB 929.55 MB
reserved data index_size unused
-- -- --
--
72136 KB 54712 KB 9544 KB 7880 KB
Now if I execute the query suggested by you...
select ((SELECT sum(convert(dec(15),size))/128 FROM test..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM test..sysindexes))
It gives me the unallocated space as 1134.570332 MB which is not
correct as per sp_spaceused (sp_spaceused returned 929.55 MB).
I think the query specified by you does not the exact space used by the
database.
Please give me the correct query to get the unallocated space for a
database.
Thanks,
Regards,
Pramod
Razvan Socol wrote:
> Hi, Pramond
> For normal indexes (between 2 and 254), the reserved column in
> sysindexes returns the number of pages allocated for that index. A page
> has a size of 8KB. If an index is a clustered index (indid=1), then it
> also contains the number of pages allocated for the data in the table.
> If a table has no index, there is a row in sysindexes with indid=0,
> which returns the number of pages allocated for all the data in the
> table. For tables that contain text and image data, there is also a row
> in sysindexes with indid=255, which returns the number of pages
> allocated for text and image data.
> In conclusion, you need to consider all indexes, not only the ones with
> indid in (0,1,255), so try:
> select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
> - (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
> The above query returns the size (in MB) of unallocated pages. You may
> have more unused space in the allocated data pages, which is not
> returned by the above query.
> Razvan
> ipramod@.gmail.com wrote:
> > Hi,
> >
> > I have below SQL Qurey which returns the used space for the database,
> > SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> > indid in (0,1,255)
> >
> > The above query returns the value in MB's or kb's which I am unable to
> > verify ?
> >
> > Below is another SQL query which returns the database size in MB's
> > which I have verified with sp_spaceused.
> > SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> >
> > If the above queries are correct then if I calculate the free space, I
> > get incorrect results.
> > FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> > master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> > master..sysindexes WHERE indid in (0,1,255)))
> >
> > I have crosschecked the same with sp_spaceused. Is anything wrong here?
> >
> > Thanks,
> > Regards,
> > Pramod|||If you want the value returned by sp_spaceused, then use somethink like
this (adapted from the code found in sp_spaceused):
select ltrim(str(((
select sum(convert(dec(15),size))
from dbo.sysfiles
where (status & 64 = 0)
) - (
select sum(convert(dec(15),reserved))
from sysindexes
where indid in (0, 1, 255)
)) / (
select 1048576 / low
from master.dbo.spt_values
where number = 1 and type = 'E'
),15,2)+ ' MB')
Razvan|||Hi Razvan,
I have sorted out the issue with the help of storedproc sp_spaceused
Below is the query to get the database size, unallocated size and the %
free space and all in MB's
declare @.pages bigint
,@.dbname sysname
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
then size else 0 end))
from dbo.sysfiles
select @.reservedpages = sum(a.total_pages)
from sys.partitions p join sys.allocation_units a on p.partition_id =a.container_id
left join sys.internal_tables it on p.object_id = it.object_id
select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
(15,2),@.logsize))/128.00
select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
(15,2),@.reservedpages)) * 8192 / 1048576
select 'DatabaseSize(MB)'=@.totalsize,
'UnallocatedSize(MB)'=@.unallocatedsize,
'DatabaseFreeSpaceInPercent(MB)'=(@.unallocatedsize*1.00/@.totalsize)*100.00
Please let me know if I am wrong.
Thanks,
Regards,
Pramod
Razvan Socol wrote:
> If you want the value returned by sp_spaceused, then use somethink like
> this (adapted from the code found in sp_spaceused):
> select ltrim(str(((
> select sum(convert(dec(15),size))
> from dbo.sysfiles
> where (status & 64 = 0)
> ) - (
> select sum(convert(dec(15),reserved))
> from sysindexes
> where indid in (0, 1, 255)
> )) / (
> select 1048576 / low
> from master.dbo.spt_values
> where number = 1 and type = 'E'
> ),15,2)+ ' MB')
> Razvan|||Hi Razvan,
Thanks for your feedback.
Now, I have another question.
I have a variable @.dbsize to which I am assigning the value of database
size and I am using the variable value in the code
Below is my SQL query which returns the database free space in percent
for all the databases.
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[dbsize] [varchar] (1000),
[logsize] [varchar] (1000)
)
DELETE FROM #test2
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
--
--EXEC sp_MSForeachdb 'use [?]; select
db_name();select @.dbsize =sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
@.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
0 end))FROM ?.dbo.sysfiles'
SELECT @.dbsize = sum(convert(bigint,case when status
& 64 = 0 then
size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM dbo.sysfiles
SELECT @.reservedpages = sum(a.total_pages)
FROM sys.partitions p join sys.allocation_units a on
p.partition_id
= a.container_id
left join sys.internal_tables it on p.object_id =it.object_id
SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
convert (dec
(15,2),@.logsize))/128.00
SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
- convert
(dec (15,2),@.reservedpages)) * 8192 / 1048576
--
SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Now this code returns the free space value in percent only for one
database because I am unable to substitute the database name when I
calculate the @.dbsize.
Can you help me?
Thanks,
Regards,
Pramod
ipramod@.gmail.com wrote:
> Hi Razvan,
> I have sorted out the issue with the help of storedproc sp_spaceused
> Below is the query to get the database size, unallocated size and the %
> free space and all in MB's
> declare @.pages bigint
> ,@.dbname sysname
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
> select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
> else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
> then size else 0 end))
> from dbo.sysfiles
> select @.reservedpages = sum(a.total_pages)
> from sys.partitions p join sys.allocation_units a on p.partition_id => a.container_id
> left join sys.internal_tables it on p.object_id = it.object_id
> select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
> (15,2),@.logsize))/128.00
> select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
> (15,2),@.reservedpages)) * 8192 / 1048576
> select 'DatabaseSize(MB)'=@.totalsize,
> 'UnallocatedSize(MB)'=@.unallocatedsize,
> 'DatabaseFreeSpaceInPercent(MB)'=(@.unallocatedsize*1.00/@.totalsize)*100.00
>
> Please let me know if I am wrong.
> Thanks,
> Regards,
> Pramod
>
> Razvan Socol wrote:
> > If you want the value returned by sp_spaceused, then use somethink like
> > this (adapted from the code found in sp_spaceused):
> >
> > select ltrim(str(((
> > select sum(convert(dec(15),size))
> > from dbo.sysfiles
> > where (status & 64 = 0)
> > ) - (
> > select sum(convert(dec(15),reserved))
> > from sysindexes
> > where indid in (0, 1, 255)
> > )) / (
> > select 1048576 / low
> > from master.dbo.spt_values
> > where number = 1 and type = 'E'
> > ),15,2)+ ' MB')
> >
> > Razvan|||Hi,
I have sorted out the issue by using the temporary tables. I have used
your suggestion and in the 'exec' itself I have inserted the variable
values in the temporary table and it worked. Thanks for your feedback
guys :)
I am copying the solution here, plz take a look and let me know if I am
wrong and if possible give me another solution. Also, can you tell me
is there any disadvantages of having temp tables in the query?
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
DELETE FROM #test2
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[DatabaseSize] [bigint],
[LogSize] [bigint]
)
DELETE FROM #test3
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize float
,@.totalsize float
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.sql = N'DECLARE @.dbsize1 bigint,@.logsize1
bigint;
SELECT @.dbsize1 = sum(convert(bigint,case when
status & 64 = 0 then
size else 0 end)), @.logsize1 = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM ['+ @.DBname +'].dbo.sysfiles;
INSERT INTO #test3 SELECT @.dbsize1, @.logsize1;'
EXEC sp_executesql @.sql
SELECT @.dbsize=[DatabaseSize], @.logsize=[LogSize]
FROM #test3
SET @.sql = N'DECLARE @.reservedpages1 bigint;
SELECT @.reservedpages1 = sum(a.total_pages)
FROM ['+ @.DBname +'].sys.partitions p join ['+
@.DBname
+'].sys.allocation_units a on p.partition_id = a.container_id
left join ['+ @.DBname +'].sys.internal_tables
it on p.object_id =it.object_id;
INSERT INTO #test3 SELECT @.reservedpages1, 0;'
EXEC sp_executesql @.sql
SELECT @.reservedpages=[DatabaseSize] FROM #test3
SELECT @.totalsize=(convert (dec (15,2),@.dbsize)*1.00
+ convert (dec
(15,2),@.logsize))*1.00/128.00
SELECT @.unallocatedsize=(convert (dec
(15,2),@.dbsize)*1.00 -
convert (dec (15,2),@.reservedpages)*1.00) * 8192.00 / 1048576.00
SET @.str =str((@.unallocatedsize*1.00/@.totalsize*1.00)*100.00, 15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Thanks,
Pramod
ipramod@.gmail.com wrote:
> Hi Razvan,
> Thanks for your feedback.
> Now, I have another question.
> I have a variable @.dbsize to which I am assigning the value of database
> size and I am using the variable value in the code
> Below is my SQL query which returns the database free space in percent
> for all the databases.
> SET nocount on
> DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
> master..sysdatabases
>
> OPEN AllDatabaseInfo
>
> IF object_id('tempdb..#test2') IS NOT NULL
> BEGIN
> DROP TABLE #test2
> END
>
> CREATE TABLE #test2 (
> [Database Name] [varchar] (1000),
> [Database Space Available] [varchar] (1000)
> )
>
> IF object_id('tempdb..#test3') IS NOT NULL
> BEGIN
> DROP TABLE #test3
> END
>
> CREATE TABLE #test3 (
> [dbsize] [varchar] (1000),
> [logsize] [varchar] (1000)
> )
>
> DELETE FROM #test2
> DECLARE @.DBName nvarchar(1000)
> DECLARE @.sql nvarchar(1000)
> DECLARE @.str sysname
> SET @.sql = ''
> SET @.DBName = ''
> DECLARE @.pages bigint
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
>
> FETCH NEXT FROM AllDatabaseInfo into @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> --
> --EXEC sp_MSForeachdb 'use [?]; select
> db_name();select @.dbsize => sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
> @.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
> 0 end))FROM ?.dbo.sysfiles'
>
> SELECT @.dbsize = sum(convert(bigint,case when status
> & 64 = 0 then
> size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
> <> 0 then size else 0 end))
> FROM dbo.sysfiles
>
> SELECT @.reservedpages = sum(a.total_pages)
> FROM sys.partitions p join sys.allocation_units a on
> p.partition_id
> = a.container_id
> left join sys.internal_tables it on p.object_id => it.object_id
>
> SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
> convert (dec
> (15,2),@.logsize))/128.00
> SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
> - convert
> (dec (15,2),@.reservedpages)) * 8192 / 1048576
> --
>
> SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
> 15,2)
> SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
> + ''', ' +
> @.str
> EXEC sp_executesql @.sql
> FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
> END
>
> CLOSE AllDatabaseInfo
> DEALLOCATE AllDatabaseInfo
>
> SELECT * FROM #test2
> SET nocount off
>
> Now this code returns the free space value in percent only for one
> database because I am unable to substitute the database name when I
> calculate the @.dbsize.
>
> Can you help me?
>
> Thanks,
> Regards,
> Pramod
>
> ipramod@.gmail.com wrote:
> > Hi Razvan,
> >
> > I have sorted out the issue with the help of storedproc sp_spaceused
> >
> > Below is the query to get the database size, unallocated size and the %
> > free space and all in MB's
> >
> > declare @.pages bigint
> > ,@.dbname sysname
> > ,@.dbsize bigint
> > ,@.logsize bigint
> > ,@.reservedpages bigint
> > ,@.unallocatedsize bigint
> > ,@.totalsize bigint
> >
> > select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
> > else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
> > then size else 0 end))
> > from dbo.sysfiles
> >
> > select @.reservedpages = sum(a.total_pages)
> > from sys.partitions p join sys.allocation_units a on p.partition_id => > a.container_id
> > left join sys.internal_tables it on p.object_id = it.object_id
> >
> > select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
> > (15,2),@.logsize))/128.00
> > select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
> > (15,2),@.reservedpages)) * 8192 / 1048576
> >
> > select 'DatabaseSize(MB)'=@.totalsize,
> > 'UnallocatedSize(MB)'=@.unallocatedsize,
> > 'DatabaseFreeSpaceInPercent(MB)'=(@.unallocatedsize*1.00/@.totalsize)*100.00
> >
> >
> > Please let me know if I am wrong.
> >
> > Thanks,
> > Regards,
> > Pramod
> >
> >
> > Razvan Socol wrote:
> > > If you want the value returned by sp_spaceused, then use somethink like
> > > this (adapted from the code found in sp_spaceused):
> > >
> > > select ltrim(str(((
> > > select sum(convert(dec(15),size))
> > > from dbo.sysfiles
> > > where (status & 64 = 0)
> > > ) - (
> > > select sum(convert(dec(15),reserved))
> > > from sysindexes
> > > where indid in (0, 1, 255)
> > > )) / (
> > > select 1048576 / low
> > > from master.dbo.spt_values
> > > where number = 1 and type = 'E'
> > > ),15,2)+ ' MB')
> > >
> > > Razvan|||For SQL Server 2005, I would use something like this:
select ltrim(str(
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
,15,2)+ ' MB')
from (
select sum(convert(dec(15,2),size)) as dbsize
from dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from sys.allocation_units
) y
If you need the unallocated space for all databases, you can use this:
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#AllDatabaseInfo') IS NOT NULL
DROP TABLE #AllDatabaseInfo
CREATE TABLE #AllDatabaseInfo (
DatabaseName sysname PRIMARY KEY,
UnallocatedSpace decimal(15,2) NOT NULL
)
DECLARE AllDatabaseInfo CURSOR LOCAL FOR
SELECT name FROM master..sysdatabases
OPEN AllDatabaseInfo
DECLARE @.DBName sysname, @.SQL nvarchar(4000)
WHILE 1=1 BEGIN
FETCH NEXT FROM AllDatabaseInfo into @.DBName
IF @.@.FETCH_STATUS<>0 BREAK
SET @.SQL = N'insert into #AllDatabaseInfo
select '+QUOTENAME(@.DBName,'''')+',
convert(dec(15,2),
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
)
from (
select sum(convert(dec(15,2),size)) as dbsize
from '+QUOTENAME(@.DBName)+'.dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from '+QUOTENAME(@.DBName)+'.sys.allocation_units
) y
'
--PRINT @.SQL
EXEC (@.SQL)
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SET NOCOUNT OFF
SELECT * FROM #AllDatabaseInfo ORDER BY DatabaseName
Razvan

Database free space

I noticed a database was showing 0 mb of free space even though I see 7 gb of
free space when I go to shrink. I added another 10 gb and I still see 0 mb of
free space. Is this a bug?Try executing DBCC UPDATEUSAGE - you can find more
information on the command in books online.
Then check the space after you have executed the command.
-Sue
On Tue, 9 Nov 2004 13:19:07 -0800, "Derek"
<evans561@.hotmail.com> wrote:
>I noticed a database was showing 0 mb of free space even though I see 7 gb of
>free space when I go to shrink. I added another 10 gb and I still see 0 mb of
>free space. Is this a bug?

Database Free Space

Hi,
I have below SQL Qurey which returns the used space for the database,
SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
indid in (0,1,255)
The above query returns the value in MB's or kb's which I am unable to
verify ?
Below is another SQL query which returns the database size in MB's
which I have verified with sp_spaceused.
SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
If the above queries are correct then if I calculate the free space, I
get incorrect results.
FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
master..sysindexes WHERE indid in (0,1,255)))
I have crosschecked the same with sp_spaceused. Is anything wrong here?
Thanks,
Regards,
PramodHi
Have you looked at the source code for sp_spaceused as you seem to be trying
to replicate what it does!
John
"ipramod@.gmail.com" wrote:

> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod
>|||Hi, Pramond
For normal indexes (between 2 and 254), the reserved column in
sysindexes returns the number of pages allocated for that index. A page
has a size of 8KB. If an index is a clustered index (indid=1), then it
also contains the number of pages allocated for the data in the table.
If a table has no index, there is a row in sysindexes with indid=0,
which returns the number of pages allocated for all the data in the
table. For tables that contain text and image data, there is also a row
in sysindexes with indid=255, which returns the number of pages
allocated for text and image data.
In conclusion, you need to consider all indexes, not only the ones with
indid in (0,1,255), so try:
select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
The above query returns the size (in MB) of unallocated pages. You may
have more unused space in the allocated data pages, which is not
returned by the above query.
Razvan
ipramod@.gmail.com wrote:
> Hi,
> I have below SQL Qurey which returns the used space for the database,
> SELECT sum(convert(dec(15), reserved)) FROM DBNAME..sysindexes WHERE
> indid in (0,1,255)
> The above query returns the value in MB's or kb's which I am unable to
> verify ?
> Below is another SQL query which returns the database size in MB's
> which I have verified with sp_spaceused.
> SELECT sum(convert(dec(15),size))/128 FROM DBNAME..sysfiles
> If the above queries are correct then if I calculate the free space, I
> get incorrect results.
> FreeSpace = select ((SELECT sum(convert(dec(15),size))/128 FROM
> master..sysfiles) - (SELECT sum(convert(dec(15), reserved)) FROM
> master..sysindexes WHERE indid in (0,1,255)))
> I have crosschecked the same with sp_spaceused. Is anything wrong here?
> Thanks,
> Regards,
> Pramod|||Hi
Thanks for your feedback.
I think the value returned by the query specified by you, still does
not give the correct result as per sp_spaceused.
Suppose I have a test database for which I execute sp_spaceused which
is as below:
database_name database_size unallocated space
----
test 1200.00 MB 929.55 MB
reserved data index_size unused
-- -- --
--
72136 KB 54712 KB 9544 KB 7880 KB
Now if I execute the query suggested by you...
select ((SELECT sum(convert(dec(15),size))/128 FROM test..sysfiles)
- (SELECT sum(convert(dec(15), reserved)/128) FROM test..sysindexes))
It gives me the unallocated space as 1134.570332 MB which is not
correct as per sp_spaceused (sp_spaceused returned 929.55 MB).
I think the query specified by you does not the exact space used by the
database.
Please give me the correct query to get the unallocated space for a
database.
Thanks,
Regards,
Pramod
Razvan Socol wrote:[vbcol=seagreen]
> Hi, Pramond
> For normal indexes (between 2 and 254), the reserved column in
> sysindexes returns the number of pages allocated for that index. A page
> has a size of 8KB. If an index is a clustered index (indid=1), then it
> also contains the number of pages allocated for the data in the table.
> If a table has no index, there is a row in sysindexes with indid=0,
> which returns the number of pages allocated for all the data in the
> table. For tables that contain text and image data, there is also a row
> in sysindexes with indid=255, which returns the number of pages
> allocated for text and image data.
> In conclusion, you need to consider all indexes, not only the ones with
> indid in (0,1,255), so try:
> select ((SELECT sum(convert(dec(15),size))/128 FROM master..sysfiles)
> - (SELECT sum(convert(dec(15), reserved)/128) FROM master..sysindexes))
> The above query returns the size (in MB) of unallocated pages. You may
> have more unused space in the allocated data pages, which is not
> returned by the above query.
> Razvan
> ipramod@.gmail.com wrote:|||If you want the value returned by sp_spaceused, then use somethink like
this (adapted from the code found in sp_spaceused):
select ltrim(str(((
select sum(convert(dec(15),size))
from dbo.sysfiles
where (status & 64 = 0)
) - (
select sum(convert(dec(15),reserved))
from sysindexes
where indid in (0, 1, 255)
)) / (
select 1048576 / low
from master.dbo.spt_values
where number = 1 and type = 'E'
),15,2)+ ' MB')
Razvan|||Hi Razvan,
I have sorted out the issue with the help of storedproc sp_spaceused
Below is the query to get the database size, unallocated size and the %
free space and all in MB's
declare @.pages bigint
,@.dbname sysname
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
then size else 0 end))
from dbo.sysfiles
select @.reservedpages = sum(a.total_pages)
from sys.partitions p join sys.allocation_units a on p.partition_id =
a.container_id
left join sys.internal_tables it on p.object_id = it.object_id
select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
(15,2),@.logsize))/128.00
select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
(15,2),@.reservedpages)) * 8192 / 1048576
select 'DatabaseSize(MB)'=@.totalsize,
'UnallocatedSize(MB)'=@.unallocatedsize,
'DatabaseFreeSpaceInPercent(MB)'=(@.unall
ocatedsize*1.00/@.totalsize)*100.00
Please let me know if I am wrong.
Thanks,
Regards,
Pramod
Razvan Socol wrote:
> If you want the value returned by sp_spaceused, then use somethink like
> this (adapted from the code found in sp_spaceused):
> select ltrim(str(((
> select sum(convert(dec(15),size))
> from dbo.sysfiles
> where (status & 64 = 0)
> ) - (
> select sum(convert(dec(15),reserved))
> from sysindexes
> where indid in (0, 1, 255)
> )) / (
> select 1048576 / low
> from master.dbo.spt_values
> where number = 1 and type = 'E'
> ),15,2)+ ' MB')
> Razvan|||Hi Razvan,
Thanks for your feedback.
Now, I have another question.
I have a variable @.dbsize to which I am assigning the value of database
size and I am using the variable value in the code
Below is my SQL query which returns the database free space in percent
for all the databases.
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[dbsize] [varchar] (1000),
[logsize] [varchar] (1000)
)
DELETE FROM #test2
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize bigint
,@.totalsize bigint
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
--
--EXEC sp_MSForeachdb 'use [?]; select
db_name();select @.dbsize =
sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
@.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
0 end))FROM ?.dbo.sysfiles'
SELECT @.dbsize = sum(convert(bigint,case when status
& 64 = 0 then
size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM dbo.sysfiles
SELECT @.reservedpages = sum(a.total_pages)
FROM sys.partitions p join sys.allocation_units a on
p.partition_id
= a.container_id
left join sys.internal_tables it on p.object_id =
it.object_id
SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
convert (dec
(15,2),@.logsize))/128.00
SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
- convert
(dec (15,2),@.reservedpages)) * 8192 / 1048576
--
SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Now this code returns the free space value in percent only for one
database because I am unable to substitute the database name when I
calculate the @.dbsize.
Can you help me?
Thanks,
Regards,
Pramod
ipramod@.gmail.com wrote:[vbcol=seagreen]
> Hi Razvan,
> I have sorted out the issue with the help of storedproc sp_spaceused
> Below is the query to get the database size, unallocated size and the %
> free space and all in MB's
> declare @.pages bigint
> ,@.dbname sysname
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
> select @.dbsize = sum(convert(bigint,case when status & 64 = 0 then size
> else 0 end)), @.logsize = sum(convert(bigint,case when status & 64 <> 0
> then size else 0 end))
> from dbo.sysfiles
> select @.reservedpages = sum(a.total_pages)
> from sys.partitions p join sys.allocation_units a on p.partition_id =
> a.container_id
> left join sys.internal_tables it on p.object_id = it.object_id
> select @.totalsize=(convert (dec (15,2),@.dbsize) + convert (dec
> (15,2),@.logsize))/128.00
> select @.unallocatedsize=(convert (dec (15,2),@.dbsize) - convert (dec
> (15,2),@.reservedpages)) * 8192 / 1048576
> select 'DatabaseSize(MB)'=@.totalsize,
> 'UnallocatedSize(MB)'=@.unallocatedsize,
> 'DatabaseFreeSpaceInPercent(MB)'=(@.unall
ocatedsize*1.00/@.totalsize)*100.00
>
> Please let me know if I am wrong.
> Thanks,
> Regards,
> Pramod
>
> Razvan Socol wrote:|||Hi,
I have sorted out the issue by using the temporary tables. I have used
your suggestion and in the 'exec' itself I have inserted the variable
values in the temporary table and it worked. Thanks for your feedback
guys
I am copying the solution here, plz take a look and let me know if I am
wrong and if possible give me another solution. Also, can you tell me
is there any disadvantages of having temp tables in the query?
SET nocount on
DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
master..sysdatabases
OPEN AllDatabaseInfo
IF object_id('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
CREATE TABLE #test2 (
[Database Name] [varchar] (1000),
[Database Space Available] [varchar] (1000)
)
DELETE FROM #test2
IF object_id('tempdb..#test3') IS NOT NULL
BEGIN
DROP TABLE #test3
END
CREATE TABLE #test3 (
[DatabaseSize] [bigint],
[LogSize] [bigint]
)
DELETE FROM #test3
DECLARE @.DBName nvarchar(1000)
DECLARE @.sql nvarchar(1000)
DECLARE @.str sysname
SET @.sql = ''
SET @.DBName = ''
DECLARE @.pages bigint
,@.dbsize bigint
,@.logsize bigint
,@.reservedpages bigint
,@.unallocatedsize float
,@.totalsize float
FETCH NEXT FROM AllDatabaseInfo into @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.sql = N'DECLARE @.dbsize1 bigint,@.logsize1
bigint;
SELECT @.dbsize1 = sum(convert(bigint,case when
status & 64 = 0 then
size else 0 end)), @.logsize1 = sum(convert(bigint,case when status & 64
<> 0 then size else 0 end))
FROM ['+ @.DBname +'].dbo.sysfiles;
INSERT INTO #test3 SELECT @.dbsize1, @.logsize1;'
EXEC sp_executesql @.sql
SELECT @.dbsize=[DatabaseSize], @.logsize=[LogSize]
FROM #test3
SET @.sql = N'DECLARE @.reservedpages1 bigint;
SELECT @.reservedpages1 = sum(a.total_pages)
FROM ['+ @.DBname +'].sys.partitions p join ['+
@.DBname
+'].sys.allocation_units a on p.partition_id = a.container_id
left join ['+ @.DBname +'].sys.internal_tables
it on p.object_id =
it.object_id;
INSERT INTO #test3 SELECT @.reservedpages1, 0;'
EXEC sp_executesql @.sql
SELECT @.reservedpages=[DatabaseSize] FROM #test3
SELECT @.totalsize=(convert (dec (15,2),@.dbsize)*1.00
+ convert (dec
(15,2),@.logsize))*1.00/128.00
SELECT @.unallocatedsize=(convert (dec
(15,2),@.dbsize)*1.00 -
convert (dec (15,2),@.reservedpages)*1.00) * 8192.00 / 1048576.00
SET @.str =
str((@.unallocatedsize*1.00/@.totalsize*1.00)*100.00, 15,2)
SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
+ ''', ' +
@.str
EXEC sp_executesql @.sql
FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SELECT * FROM #test2
SET nocount off
Thanks,
Pramod
ipramod@.gmail.com wrote:[vbcol=seagreen]
> Hi Razvan,
> Thanks for your feedback.
> Now, I have another question.
> I have a variable @.dbsize to which I am assigning the value of database
> size and I am using the variable value in the code
> Below is my SQL query which returns the database free space in percent
> for all the databases.
> SET nocount on
> DECLARE AllDatabaseInfo CURSOR LOCAL FOR SELECT name FROM
> master..sysdatabases
>
> OPEN AllDatabaseInfo
>
> IF object_id('tempdb..#test2') IS NOT NULL
> BEGIN
> DROP TABLE #test2
> END
>
> CREATE TABLE #test2 (
> [Database Name] [varchar] (1000),
> [Database Space Available] [varchar] (1000)
> )
>
> IF object_id('tempdb..#test3') IS NOT NULL
> BEGIN
> DROP TABLE #test3
> END
>
> CREATE TABLE #test3 (
> [dbsize] [varchar] (1000),
> [logsize] [varchar] (1000)
> )
>
> DELETE FROM #test2
> DECLARE @.DBName nvarchar(1000)
> DECLARE @.sql nvarchar(1000)
> DECLARE @.str sysname
> SET @.sql = ''
> SET @.DBName = ''
> DECLARE @.pages bigint
> ,@.dbsize bigint
> ,@.logsize bigint
> ,@.reservedpages bigint
> ,@.unallocatedsize bigint
> ,@.totalsize bigint
>
> FETCH NEXT FROM AllDatabaseInfo into @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> --
> --EXEC sp_MSForeachdb 'use [?]; select
> db_name();select @.dbsize =
> sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
> @.logsize = sum(convert(bigint,case when status & 64 <> 0 then size else
> 0 end))FROM ?.dbo.sysfiles'
>
> SELECT @.dbsize = sum(convert(bigint,case when status
> & 64 = 0 then
> size else 0 end)), @.logsize = sum(convert(bigint,case when status & 64
> <> 0 then size else 0 end))
> FROM dbo.sysfiles
>
> SELECT @.reservedpages = sum(a.total_pages)
> FROM sys.partitions p join sys.allocation_units a on
> p.partition_id
> = a.container_id
> left join sys.internal_tables it on p.object_id =
> it.object_id
>
> SELECT @.totalsize=(convert (dec (15,2),@.dbsize) +
> convert (dec
> (15,2),@.logsize))/128.00
> SELECT @.unallocatedsize=(convert (dec (15,2),@.dbsize)
> - convert
> (dec (15,2),@.reservedpages)) * 8192 / 1048576
> --
>
> SET @.str = str((@.unallocatedsize*1.00/@.totalsize)*100.00,
> 15,2)
> SET @.sql = N'INSERT INTO #test2 SELECT ''' + @.DBName
> + ''', ' +
> @.str
> EXEC sp_executesql @.sql
> FETCH NEXT FROM AllDatabaseInfo INTO @.DBName
> END
>
> CLOSE AllDatabaseInfo
> DEALLOCATE AllDatabaseInfo
>
> SELECT * FROM #test2
> SET nocount off
>
> Now this code returns the free space value in percent only for one
> database because I am unable to substitute the database name when I
> calculate the @.dbsize.
>
> Can you help me?
>
> Thanks,
> Regards,
> Pramod
>
> ipramod@.gmail.com wrote:|||For SQL Server 2005, I would use something like this:
select ltrim(str(
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
,15,2)+ ' MB')
from (
select sum(convert(dec(15,2),size)) as dbsize
from dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from sys.allocation_units
) y
If you need the unallocated space for all databases, you can use this:
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#AllDatabaseInfo') IS NOT NULL
DROP TABLE #AllDatabaseInfo
CREATE TABLE #AllDatabaseInfo (
DatabaseName sysname PRIMARY KEY,
UnallocatedSpace decimal(15,2) NOT NULL
)
DECLARE AllDatabaseInfo CURSOR LOCAL FOR
SELECT name FROM master..sysdatabases
OPEN AllDatabaseInfo
DECLARE @.DBName sysname, @.SQL nvarchar(4000)
WHILE 1=1 BEGIN
FETCH NEXT FROM AllDatabaseInfo into @.DBName
IF @.@.FETCH_STATUS<>0 BREAK
SET @.SQL = N'insert into #AllDatabaseInfo
select '+QUOTENAME(@.DBName,'''')+',
convert(dec(15,2),
case
when reservedpages<dbsize
then dbsize-reservedpages
else 0
end * 8192 / 1048576
)
from (
select sum(convert(dec(15,2),size)) as dbsize
from '+QUOTENAME(@.DBName)+'.dbo.sysfiles
where (status & 64 = 0)
) x, (
select sum(convert(dec(15,2),total_pages)) as reservedpages
from '+QUOTENAME(@.DBName)+'.sys.allocation_units
) y
'
--PRINT @.SQL
EXEC (@.SQL)
END
CLOSE AllDatabaseInfo
DEALLOCATE AllDatabaseInfo
SET NOCOUNT OFF
SELECT * FROM #AllDatabaseInfo ORDER BY DatabaseName
Razvansql

Database free space

I noticed a database was showing 0 mb of free space even though I see 7 gb o
f
free space when I go to shrink. I added another 10 gb and I still see 0 mb o
f
free space. Is this a bug?Try executing DBCC UPDATEUSAGE - you can find more
information on the command in books online.
Then check the space after you have executed the command.
-Sue
On Tue, 9 Nov 2004 13:19:07 -0800, "Derek"
<evans561@.hotmail.com> wrote:

>I noticed a database was showing 0 mb of free space even though I see 7 gb
of
>free space when I go to shrink. I added another 10 gb and I still see 0 mb
of
>free space. Is this a bug?

Database files free space (SQL Server 2005)

Dear friends

I need to report the amount of free space in each datafile of a database.

I have tried sys.dm_db_file_space_usage , but the documentation says that it only works for the TempDB.

Please help.

Regards

Parviz

Take a look at the sp_spaceused text. You should be able to derive from there.

exec sp_helptext 'sp_spaceused'|||

Thanks for the fast reply, but please note that sp_spaceused returns the free space for the whole database files including the LOG file. I need the info fo each file in a database seperately.

If you have a look at summary reports for a database (SQL 2005 SSMS),you would see a table and a pie chart showng the status for each file.

Thanks again.

Parviz

|||Yup. sp_spacedused can be used to get the data file usage. You can then use 'dbcc sqlperf(logspace)' to get the log usage.|||

Here's some code, cribbed from a sqldbatips report designed to mimic the taskpad in SSMS. (http://sqlblogcasts.com/blogs/sqldbatips/archive/2006/11/21/custom-ssms-reports-in-sp2-enterprise-manager-taskpad-view.aspx). All I did was add some code to show the amount free. Seems to work well. Posting here because this was one of the top results in Google when I went looking for code myself.

create table #data(Fileid int NOT NULL,

[FileGroup] int NOT NULL,

TotalExtents int NOT NULL,

UsedExtents int NOT NULL,

[Name] sysname NOT NULL,

[FileName] varchar(300) NOT NULL)

create table #log(dbname sysname NOT NULL,

LogSize numeric(15,7) NOT NULL,

LogUsed numeric(9,5) NOT NULL,

Status int NOT NULL)

insert #data exec('DBCC showfilestats with no_infomsgs')

insert #log exec('dbcc sqlperf(logspace) with no_infomsgs')

select [type], [name], totalmb, usedmb, totalmb - usedmb as EmptySpace from

(

select 'DATA' as [Type],[Name],(TotalExtents*64)/1024.0 as [TotalMB],(UsedExtents*64)/1024.0 as [UsedMB]

from #data

union all

select 'LOG',db_name()+' LOG',LogSize,((LogUsed/100)*LogSize) from #log where dbname = db_name()

--order by [Type],[Name]

)a

order by [Type],[Name]

drop table #data

drop table #log

|||

This will work for you:

-- Individual File Size query

SELECT name AS 'File Name' , physical_name AS 'Physical Name', size/128 AS 'Total Size in MB',

size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB', *

FROM sys.database_files;

Database files free space (SQL Server 2005)

Dear friends

I need to report the amount of free space in each datafile of a database.

I have tried sys.dm_db_file_space_usage , but the documentation says that it only works for the TempDB.

Please help.

Regards

Parviz

Take a look at the sp_spaceused text. You should be able to derive from there.

exec sp_helptext 'sp_spaceused'|||

Thanks for the fast reply, but please note that sp_spaceused returns the free space for the whole database files including the LOG file. I need the info fo each file in a database seperately.

If you have a look at summary reports for a database (SQL 2005 SSMS),you would see a table and a pie chart showng the status for each file.

Thanks again.

Parviz

|||Yup. sp_spacedused can be used to get the data file usage. You can then use 'dbcc sqlperf(logspace)' to get the log usage.|||

Here's some code, cribbed from a sqldbatips report designed to mimic the taskpad in SSMS. (http://sqlblogcasts.com/blogs/sqldbatips/archive/2006/11/21/custom-ssms-reports-in-sp2-enterprise-manager-taskpad-view.aspx). All I did was add some code to show the amount free. Seems to work well. Posting here because this was one of the top results in Google when I went looking for code myself.

create table #data(Fileid int NOT NULL,

[FileGroup] int NOT NULL,

TotalExtents int NOT NULL,

UsedExtents int NOT NULL,

[Name] sysname NOT NULL,

[FileName] varchar(300) NOT NULL)

create table #log(dbname sysname NOT NULL,

LogSize numeric(15,7) NOT NULL,

LogUsed numeric(9,5) NOT NULL,

Status int NOT NULL)

insert #data exec('DBCC showfilestats with no_infomsgs')

insert #log exec('dbcc sqlperf(logspace) with no_infomsgs')

select [type], [name], totalmb, usedmb, totalmb - usedmb as EmptySpace from

(

select 'DATA' as [Type],[Name],(TotalExtents*64)/1024.0 as [TotalMB],(UsedExtents*64)/1024.0 as [UsedMB]

from #data

union all

select 'LOG',db_name()+' LOG',LogSize,((LogUsed/100)*LogSize) from #log where dbname = db_name()

--order by [Type],[Name]

)a

order by [Type],[Name]

drop table #data

drop table #log

|||

This will work for you:

-- Individual File Size query

SELECT name AS 'File Name' , physical_name AS 'Physical Name', size/128 AS 'Total Size in MB',

size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB', *

FROM sys.database_files;

Thursday, March 22, 2012

Database File and Disk Space

Hi All,
One of the drives that stores the database file is close to running out of space. The chance of me getting more space added to this drive any time soon are really low. What are other options I have?
Thanks.1. Open a new file segment on another drive
2. Shrink the existing log and or data file(s)
3. Tell TPTB that they are going to lose the ability to use the database due to lack of disk space.

Wednesday, March 21, 2012

Database Expansion Problem

Hello:
I have an SQL2K db that is 7.5GB in size. The data files
have 0% free space. When I attempt to expand the db to
add more space, the size of the data file increases by
whatever increment I have just put in.
When I check the properties of the database after
expanding it, it still shows 0% free space. If I check
the actual size of the .mdb file, it has also increased.
But if I shrink the database, it shrinks to the original
7.5GB I've done this with another database and the
increase in the amount of what I allocate for the
database shows as free space when I check the db
properties.
Thanks for any help
BrennanTry running
USE YourDatabaseName
sp_spaceused @.updateusage='TRUE' . This will update the usage information
for the database.
Jim
"Brennan" <anonymous@.discussions.microsoft.com> wrote in message
news:027e01c3ce56$89a001e0$a601280a@.phx.gbl...
> Hello:
> I have an SQL2K db that is 7.5GB in size. The data files
> have 0% free space. When I attempt to expand the db to
> add more space, the size of the data file increases by
> whatever increment I have just put in.
> When I check the properties of the database after
> expanding it, it still shows 0% free space. If I check
> the actual size of the .mdb file, it has also increased.
> But if I shrink the database, it shrinks to the original
> 7.5GB I've done this with another database and the
> increase in the amount of what I allocate for the
> database shows as free space when I check the db
> properties.
> Thanks for any help
> Brennan

database errr,URGENT

Hi
I am using mssql7 server.unfortunatily the space of the
hard disk reached below 10mb and while saving data
server has showned the disk full error.
After making enough space i restared the server,the server
has started,but one of the main database is showing an
error.msg 945 Level 14 state 2, Some of the files
could not be activated.
i am linking some of the table from this database to
another database,how can i start this database.
Please help me, urgent
Thank you
You may have better luck asking this question in
microsoft.public.sqlserver.server. The topic of this newsgroup is
Microsoft English Query.
SK
shif wrote:

>Hi
>I am using mssql7 server.unfortunatily the space of the
>hard disk reached below 10mb and while saving data
>server has showned the disk full error.
>After making enough space i restared the server,the server
>has started,but one of the main database is showing an
>error.msg 945 Level 14 state 2, Some of the files
>could not be activated.
>i am linking some of the table from this database to
>another database,how can i start this database.
>Please help me, urgent
>Thank you
>
>

Sunday, March 11, 2012

Database disappeared from sysdatabases after running out of file space

Guys and gals, I'm in all sorts of trouble here. I've been nursing a server
with very little free space for my sql databases while I get a new one
built, and it's been pretty safe, until a power cut the other day caused
some sort of huge write to the log file and it ran out of space. I mean,
really ran out. The mdf and logs are at about 29.5 gigs and the hdd is
30gig. So, I needed to backup the transaction log, reorganise and shrink
the database. Couldn't do that, no elbow room, so I thought I'd move the
files to a USB hdd I have on there, slow but ok temporarily, and do the
reorg there. Unfortunately, detatching the database has, somehow caused the
sysdatabases table to lose any knowledge of it. I've got the files in their
new location and would dearly love to be able to reattach them somehow to
shrink them down but I'm at a loss now.
I'd really appreciate a pointer here, I've run out of ideas.
Cheers
Chris Weston"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:w9YSf.2115$g76.696@.newsfe2-gui.ntli.net...
> Guys and gals, I'm in all sorts of trouble here. I've been nursing a
server
> with very little free space for my sql databases while I get a new one
> built, and it's been pretty safe, until a power cut the other day caused
> some sort of huge write to the log file and it ran out of space. I mean,
> really ran out. The mdf and logs are at about 29.5 gigs and the hdd is
> 30gig. So, I needed to backup the transaction log, reorganise and shrink
> the database. Couldn't do that, no elbow room, so I thought I'd move the
> files to a USB hdd I have on there, slow but ok temporarily, and do the
> reorg there. Unfortunately, detatching the database has, somehow caused
the
> sysdatabases table to lose any knowledge of it. I've got the files in
their
> new location and would dearly love to be able to reattach them somehow to
> shrink them down but I'm at a loss now.
> I'd really appreciate a pointer here, I've run out of ideas.
Just to follow up here, I've tried attaching the database from the context
menu on my databases folder, but it complains about the log file (all 22g of
it) being faulty. Can't I attach the mdf and let the log file be recreated?
I'm no sql expert but the mdf is the important part, surely?
Thanks,
--
Chris Weston|||sp_attach_single_file_db [ @.dbname = ] 'dbname'
, [ @.physname = ] 'physical_name'
It will generate a new log file.
good luck.
d.
"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:tIYSf.2054$H%3.875@.newsfe5-gui.ntli.net...
> "Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
> news:w9YSf.2115$g76.696@.newsfe2-gui.ntli.net...
> > Guys and gals, I'm in all sorts of trouble here. I've been nursing a
> server
> > with very little free space for my sql databases while I get a new one
> > built, and it's been pretty safe, until a power cut the other day caused
> > some sort of huge write to the log file and it ran out of space. I
mean,
> > really ran out. The mdf and logs are at about 29.5 gigs and the hdd is
> > 30gig. So, I needed to backup the transaction log, reorganise and
shrink
> > the database. Couldn't do that, no elbow room, so I thought I'd move
the
> > files to a USB hdd I have on there, slow but ok temporarily, and do the
> > reorg there. Unfortunately, detatching the database has, somehow caused
> the
> > sysdatabases table to lose any knowledge of it. I've got the files in
> their
> > new location and would dearly love to be able to reattach them somehow
to
> > shrink them down but I'm at a loss now.
> >
> > I'd really appreciate a pointer here, I've run out of ideas.
> Just to follow up here, I've tried attaching the database from the context
> menu on my databases folder, but it complains about the log file (all 22g
of
> it) being faulty. Can't I attach the mdf and let the log file be
recreated?
> I'm no sql expert but the mdf is the important part, surely?
> Thanks,
> --
> Chris Weston
>|||"d" <d@.d.com> wrote in message news:ruZSf.1311$I7.1016@.trnddc03...
> sp_attach_single_file_db [ @.dbname = ] 'dbname'
> , [ @.physname = ] 'physical_name'
> It will generate a new log file.
>
> good luck.
>
I appreciate the sentiment :) However, I'm not sure where to run this
command? In a query window in Enterprise Manager?
Many thanks,
Chris Weston|||Hi Chris
This is expected behavior. When you detach a database, it is completely
removed from the system. It is different from DROP in that the files still
exist on disk.
To get the db back, you have to attach it.
If you are attaching through Enterprise Manager, and SQL Server complains
about the log, you can try changing the name of the log file on disk so EM
can't find it, and see if a new log will be rebuilt.
Or, as d suggested, you can run the procedure from sp_attach_single_file_db
from a query window in Query Analyzer.
--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com
"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:w9YSf.2115$g76.696@.newsfe2-gui.ntli.net...
> Guys and gals, I'm in all sorts of trouble here. I've been nursing a
> server
> with very little free space for my sql databases while I get a new one
> built, and it's been pretty safe, until a power cut the other day caused
> some sort of huge write to the log file and it ran out of space. I mean,
> really ran out. The mdf and logs are at about 29.5 gigs and the hdd is
> 30gig. So, I needed to backup the transaction log, reorganise and shrink
> the database. Couldn't do that, no elbow room, so I thought I'd move the
> files to a USB hdd I have on there, slow but ok temporarily, and do the
> reorg there. Unfortunately, detatching the database has, somehow caused
> the
> sysdatabases table to lose any knowledge of it. I've got the files in
> their
> new location and would dearly love to be able to reattach them somehow to
> shrink them down but I'm at a loss now.
> I'd really appreciate a pointer here, I've run out of ideas.
> Cheers
> Chris Weston
>
>|||"Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
news:uKXsmotSGHA.5500@.TK2MSFTNGP12.phx.gbl...
> Hi Chris
> This is expected behavior. When you detach a database, it is completely
> removed from the system. It is different from DROP in that the files still
> exist on disk.
> To get the db back, you have to attach it.
> If you are attaching through Enterprise Manager, and SQL Server complains
> about the log, you can try changing the name of the log file on disk so EM
> can't find it, and see if a new log will be rebuilt.
> Or, as d suggested, you can run the procedure from
sp_attach_single_file_db
> from a query window in Query Analyzer.
Still, no joy - I have tried both these methods but still get an error
saying that it can't find the .ldf file. How can I get past this?
Thanks,
Chris Weston|||Hi
The methods described by Kalen should work if you followed them exactly.
Can you post the exact sp_attach_single_file_db command that you have used?
Make sure that you have move or renamed the ldf file and there is enough
space to create a new one.
Make sure that the mdf file does exist in the directory you have specified.
Make sure that directory where it wants to create the ldf file exists and
that the permissions allow you to create a new file.
John
"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:dz8Tf.2219$g76.437@.newsfe2-gui.ntli.net...
> "Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
> news:uKXsmotSGHA.5500@.TK2MSFTNGP12.phx.gbl...
>> Hi Chris
>> This is expected behavior. When you detach a database, it is completely
>> removed from the system. It is different from DROP in that the files
>> still
>> exist on disk.
>> To get the db back, you have to attach it.
>> If you are attaching through Enterprise Manager, and SQL Server complains
>> about the log, you can try changing the name of the log file on disk so
>> EM
>> can't find it, and see if a new log will be rebuilt.
>> Or, as d suggested, you can run the procedure from
> xslmailinglist@.btopenworld.com>> from a query window in Query Analyzer.
> Still, no joy - I have tried both these methods but still get an error
> saying that it can't find the .ldf file. How can I get past this?
> Thanks,
> Chris Weston
>|||"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:e%23SFB3zSGHA.4600@.TK2MSFTNGP11.phx.gbl...
> Hi
> The methods described by Kalen should work if you followed them exactly.
> Can you post the exact sp_attach_single_file_db command that you have
used?
> Make sure that you have move or renamed the ldf file and there is enough
> space to create a new one.
> Make sure that the mdf file does exist in the directory you have
specified.
> Make sure that directory where it wants to create the ldf file exists and
> that the permissions allow you to create a new file.
It exists, it's my default sql data directory and I'm logged in as
administrator. The mdf exists. I'm using
EXEC sp_attach_single_file_db @.dbname = 'SysCompanyA',
@.physname = N'h:\systemp\SyscompA.mdf'
but I get an error saying that 'the physical file name 'E:\program
files\...\data\syscompanyA_log.ldf may be incorrect'
So is it saying it can't create or that it can't find the ldf file?
--
Chris Weston|||HI Chris
Was E:\ the original drive? It looks like when SQL Server creates a new log
file, it wants to create it on the drive where it originally existed.
I have use sp_attach_db to move a db, or to rebuild a log, but never at the
same time.
--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com
"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:o0hTf.2330$g76.772@.newsfe2-gui.ntli.net...
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message
> news:e%23SFB3zSGHA.4600@.TK2MSFTNGP11.phx.gbl...
>> Hi
>> The methods described by Kalen should work if you followed them exactly.
>> Can you post the exact sp_attach_single_file_db command that you have
> used?
>> Make sure that you have move or renamed the ldf file and there is enough
>> space to create a new one.
>> Make sure that the mdf file does exist in the directory you have
> specified.
>> Make sure that directory where it wants to create the ldf file exists and
>> that the permissions allow you to create a new file.
> It exists, it's my default sql data directory and I'm logged in as
> administrator. The mdf exists. I'm using
> EXEC sp_attach_single_file_db @.dbname = 'SysCompanyA',
> @.physname = N'h:\systemp\SyscompA.mdf'
> but I get an error saying that 'the physical file name 'E:\program
> files\...\data\syscompanyA_log.ldf may be incorrect'
> So is it saying it can't create or that it can't find the ldf file?
> --
> Chris Weston
>
>|||"Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
news:OUXQlB9SGHA.5172@.TK2MSFTNGP12.phx.gbl...
> HI Chris
> Was E:\ the original drive? It looks like when SQL Server creates a new
log
> file, it wants to create it on the drive where it originally existed.
> I have use sp_attach_db to move a db, or to rebuild a log, but never at
the
> same time.
Kalen,
E:\ was the original drive, and it still exists. I've no problem with it
creating the log there if need be.
--
Chris Weston|||Chris Weston wrote:
> "Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
> news:OUXQlB9SGHA.5172@.TK2MSFTNGP12.phx.gbl...
>> HI Chris
>> Was E:\ the original drive? It looks like when SQL Server creates a new
> log
>> file, it wants to create it on the drive where it originally existed.
>> I have use sp_attach_db to move a db, or to rebuild a log, but never at
> the
>> same time.
> Kalen,
> E:\ was the original drive, and it still exists. I've no problem with it
> creating the log there if need be.
>
You have to make sure that the full path/folder exists before you run
the command. SQL server can't create the folder so if it's not present
you'll get an error message like the one you see.
Regards
Steen|||So make sure there is no file on E with the name and path the log should
have. Make sure the directory exists on E and that the Windows account that
SQL Server runs under has access to write to the directory.
Once the attach succeeds, it should have a very small log file. You can then
detach it, move the new small log file to your new location, and then attach
again, specifying explicitly the log file location.
--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com
"Chris Weston" <chrisweston[losethislot]@.ntlworld.com> wrote in message
news:7msTf.11031$5B4.10504@.newsfe6-gui.ntli.net...
> "Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
> news:OUXQlB9SGHA.5172@.TK2MSFTNGP12.phx.gbl...
>> HI Chris
>> Was E:\ the original drive? It looks like when SQL Server creates a new
> log
>> file, it wants to create it on the drive where it originally existed.
>> I have use sp_attach_db to move a db, or to rebuild a log, but never at
> the
>> same time.
> Kalen,
> E:\ was the original drive, and it still exists. I've no problem with it
> creating the log there if need be.
> --
> Chris Weston
>
>|||"Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
news:uUKGRCETGHA.4740@.TK2MSFTNGP14.phx.gbl...
> So make sure there is no file on E with the name and path the log should
> have. Make sure the directory exists on E and that the Windows account
that
> SQL Server runs under has access to write to the directory.
> Once the attach succeeds, it should have a very small log file. You can
then
> detach it, move the new small log file to your new location, and then
attach
> again, specifying explicitly the log file location.
Folks, thanks very much for your efforts. I couldn't attach this db, it
just wasn't having it. I ended up restoring from a backup and we'll just
have to do the work to bring it up to date. I did eventually attach the db
using the procedure detailed here
http://www.spaceprogram.com/knowledge/2002/06/recovering-from-deleted-log-file-on_12.html
although I had to repair it using allow_data_loss, and I can use it to help
me get the restored data back to usability.
So, thanks for your thoughts on this one, but may I ask one question? Why
the consistent top-posting in this ng?
Thanks
--
Chris Weston