Domanda

I need to find the relative amount of space available in my SQL Server database using an ODBC connection.

I wanted to try using dbcc showfilestats but it does not return any results.

Having read INF: Processing DBCC Commands in ODBC applications I understood it is because this is not a select statement. What puzzled me is that the statement didn't return any info either (contrary to the paper).

I also tried using a temporary table:

declare @FileStats table
(
  Fileid int,
  [FileGroup] int,
  TotalExtents int,
  UsedExtents int,
  Name varchar(255),
  [FileName] varchar(max)
)
insert into @FileStats execute('dbcc showfilestats')

and selecting from that table in the same SQLExecDirect.

select
  1-convert(float, sum(UsedExtents))/convert(float, sum(TotalExtents))
  as FreeDataSpace from @FileStats

This only resulted in error 24000 (and I made sure there are no other open statements).

Is there an alternative way to retrieve this information?

È stato utile?

Soluzione

Apparently there's always an alternative way to do something.

What ultimately worked for me was:

SELECT convert(float, (sum(f.size) - sum(fileproperty(f.name,'SpaceUsed')))) / sum(f.size)
FROM sys.sysfiles f JOIN sys.database_files db_f ON f.fileid = db_f.file_id
WHERE db_f.type = 0

It works through ODBC and gives the same results as my original solution when executed in Management Studio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top