script to display servernames with freediskspace

  • How to use the below script using cursor to get the same output

    DECLARE @diskspace table (SNo int identity(1,1),Servername Varchar(255),Freediskspace INT)

    INSERT INTO @diskspace SELECT ServerName,Sum(FreeSpace_MB/1024) from dbo.DiskDriveInfo group by servername

    DECLARE @cnt int

    SELECT @cnt = COUNT (*) from @diskspace

    WHILE @cnt <>0

    BEGIN

    DECLARE @ServerName Varchar(255)

    DECLARE @Freediskspace INT

    DECLARE @sn Varchar(255)

    DECLARE @fds INT

    SELECT @sn = ServerName,@fds = Freediskspace from @diskspace where SNo=@cnt

    PRINT 'ServerName'+''+ 'Freediskspace'

    PRINT @sn +''+ CAST(@fds AS VARCHAR(50))

    SET @cnt =@cnt-1

    END

    OUTPUT:

    Server Name Free Disk Space(In GB)

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

    IN-WKS-101 406

    IN-SVR-396 5347

  • sinu12345 (11/26/2009)


    How to use the below script using cursor to get the same output

    DECLARE @diskspace table (SNo int identity(1,1),Servername Varchar(255),Freediskspace INT)

    INSERT INTO @diskspace SELECT ServerName,Sum(FreeSpace_MB/1024) from dbo.DiskDriveInfo group by servername

    DECLARE @cnt int

    SELECT @cnt = COUNT (*) from @diskspace

    WHILE @cnt <>0

    BEGIN

    DECLARE @ServerName Varchar(255)

    DECLARE @Freediskspace INT

    DECLARE @sn Varchar(255)

    DECLARE @fds INT

    SELECT @sn = ServerName,@fds = Freediskspace from @diskspace where SNo=@cnt

    PRINT 'ServerName'+''+ 'Freediskspace'

    PRINT @sn +''+ CAST(@fds AS VARCHAR(50))

    SET @cnt =@cnt-1

    END

    OUTPUT:

    Server Name Free Disk Space(In GB)

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

    IN-WKS-101 406

    IN-SVR-396 5347

  • Why would you need a cursor or use the PRINT statement to get this information?

    Couldn't you just do this?

    SELECT

    ServerName,

    Sum(FreeSpace_MB/1024) as [Disk Free Space (GB)]

    from

    dbo.DiskDriveInfo

    group by

    servername

  • I agree with Jack. Why do you need to loop?

Viewing 4 posts - 1 through 4 (of 4 total)

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