To find freespace of Database using a query

  • Hi,

    Thanks for youre response. I have already looked into that script. But im looking for a single query which returns the freespace of a disk drive.

    I need a single MS SQL query to find the freespace of Disk drive.

    Can anybody help me on this?

    Thanks in advance,

    MRK.

  • Gosh... what's wrong with just...

    EXEC master.dbo.xp_fixeddrives

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • HI,

    I know that the above procedure is correct. But we are facing some techincal problem in this while implementing it into the DTS Package.

    CREATE procedure DiskSpace_Size

    AS

    SET NOCOUNT ON

    DECLARE @disksize Decimal(15,2)

    SET NOCOUNT ON

    BEGIN

    CREATE TABLE #T1(DRVLETTER CHAR(1),DRVSPACE INT)

    INSERT INTO #T1 EXEC master.dbo.xp_fixeddrives;

    select @DiskSize =(SELECT DRVSPACE FROM #T1 WHERE DRVLETTER='D');

    Drop TABLE #T1

    return @DiskSize

    end

    Execution:

    ------------

    DECLARE @ReturnValue int

    EXEC @ReturnValue = DiskSpace_Size

    SELECT ReturnValue=@ReturnValue

    While executing like this im getting the freespace. But when using DTS Package.

    The Parameter "ReturnValue" is not getting the value.

    So It would be better if we find the freespace of DiskDrive in a single query.

    So that it will work fine.

    MRK.

  • You can use th script named database detais available in the site http://www.sql-articles.com

    Cheers,
    Sugeshkumar Rajendran
    SQL Server MVP
    http://sugeshkr.blogspot.com

  • Hi I have a script which i downloaded way back. Sorry i forgot author's name. But i pass on my credits to him if he finds this script.

    i modified the script little bit to suit the results i want.

    this script displays drive, total size and available size.

    DECLARE @Drive TINYINT,

    @SQL VARCHAR(100)

    SET @Drive = 97

    -- Setup Staging Area

    DECLARE @Drives TABLE

    (

    Drive CHAR(1),

    Info VARCHAR(80)

    )

    WHILE @Drive <= 122

    BEGIN

    SET @SQL = 'EXEC XP_CMDSHELL ''fsutil volume diskfree ' + CHAR(@Drive) + ':'''

    INSERT @Drives

    (

    Info

    )

    EXEC (@SQL)

    UPDATE @Drives

    SET Drive = CHAR(@Drive)

    WHERE Drive IS NULL

    SET @Drive = @Drive + 1

    END

    -- Show the expected output

    SELECT Drive,

    ((SUM(CASE WHEN Info LIKE 'Total # of bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END)/1024)/1024) AS TotalMB,

    ((SUM(CASE WHEN Info LIKE 'Total # of avail free bytes : %' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END)/1024)/1024) AS AvailFreeMB

    FROM (

    SELECT Drive,

    Info

    FROM @Drives

    WHERE Info LIKE 'Total # of %'

    ) AS d

    GROUP BY Drive

    ORDER BY Drive

    Hope this solves your purpose.

  • Hi,

    When i Try to execute

    DECLARE @Drive TINYINT,

    @SQL VARCHAR(100)

    SET @Drive = 97

    -- Setup Staging Area

    DECLARE @Drives TABLE

    (

    Drive CHAR(1),

    Info VARCHAR(80)

    )

    WHILE @Drive <= 122

    BEGIN

    SET @SQL = 'EXEC XP_CMDSHELL ''fsutil volume diskfree ' + CHAR(@Drive) + ':'''

    INSERT @Drives

    (

    Info

    )

    EXEC (@SQL)

    UPDATE @Drives

    SET Drive = CHAR(@Drive)

    WHERE Drive IS NULL

    SET @Drive = @Drive + 1

    END

    Im getting an error:

    EXECUTE cannot be used as a source when inserting into a table variable.

    pls help to resolve this issue.

  • wrap all the statments into the string and after that execute it with exec()

  • You can't insert rows into table variables in an INSERT..EXEC statement.

    Create a temporary table to hold the data instead of table variable.

    --Ramesh


  • Hi,

    Thank you very much.

    I have my procedure DiskSpace_Size in DTS Package.

    The problem is with my output parameter.

    I have resolved it.

    Thanks for all your support and information.

    MRK.

  • Two way street here... HOW did you resolve it?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Hi,

    I have implemented that Procedure using DTS Package.

    I returned the freespace from the Procedure.

    In DTS Package i got the returned value as Global Parameter.

    But im facing a problem here that Global variable is not getting updated.

    Can anybody help me on this?

    MRK.

  • ----The following will return each local drive and a free space in MBEXEC master.dbo.xp_fixeddrives

    ----The following function with obtain a supplied drive's capacity

    ALTER FUNCTION [dbo].[fnDBDriveStats_GetDriveSize]

    (@cDrive char(1))

    RETURNS int

    AS

    --Makes OLE Automation call to obtain supplied drive's total size

    BEGIN

    DECLARE @x int

    DECLARE @iFSO int

    DECLARE @iDrive int

    DECLARE @vTotalSize varchar(20)

    DECLARE @biMB bigint

    DECLARE @iSizeMB int

    SET @biMB = 1048576

    EXEC @x = sp_OACreate 'Scripting.FileSystemObject', @iFSO OUT

    IF @x <> 0

    RETURN -1

    EXEC @x = sp_OAMethod @iFSO, 'GetDrive', @iDrive OUT, @cDrive

    IF @x <> 0

    RETURN -1

    EXEC @x = sp_OAGetProperty @iDrive, 'TotalSize', @vTotalSize OUT

    IF @x <> 0

    RETURN -1

    SET @iSizeMB = @vTotalSize/@biMB

    EXEC @x = sp_OADestroy @iFSO

    RETURN @iSizeMB

    END

  • this query is excellent, to find the space in database files size.

  • good query..

  • bfranklin-557019 (12/7/2007)


    ----The following will return each local drive and a free space in MBEXEC master.dbo.xp_fixeddrives

    ----The following function with obtain a supplied drive's capacity

    ALTER FUNCTION [dbo].[fnDBDriveStats_GetDriveSize]

    (@cDrive char(1))

    RETURNS int

    AS

    --Makes OLE Automation call to obtain supplied drive's total size

    BEGIN

    DECLARE @x int

    DECLARE @iFSO int

    DECLARE @iDrive int

    DECLARE @vTotalSize varchar(20)

    DECLARE @biMB bigint

    DECLARE @iSizeMB int

    SET @biMB = 1048576

    EXEC @x = sp_OACreate 'Scripting.FileSystemObject', @iFSO OUT

    IF @x <> 0

    RETURN -1

    EXEC @x = sp_OAMethod @iFSO, 'GetDrive', @iDrive OUT, @cDrive

    IF @x <> 0

    RETURN -1

    EXEC @x = sp_OAGetProperty @iDrive, 'TotalSize', @vTotalSize OUT

    IF @x <> 0

    RETURN -1

    SET @iSizeMB = @vTotalSize/@biMB

    EXEC @x = sp_OADestroy @iFSO

    RETURN @iSizeMB

    END

    Just a quick question on that script... why don't you destroy the @iFSO object when an error occurs just before the second and third "RETURN -1"?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 15 posts - 16 through 30 (of 30 total)

You must be logged in to reply to this topic. Login to reply