November 26, 2009 at 10:03 pm
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
November 30, 2009 at 4:49 am
sinu12345 (11/26/2009)
How to use the below script using cursor to get the same outputDECLARE @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
November 30, 2009 at 6:51 am
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
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
November 30, 2009 at 8:59 am
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