Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Stored Procedure: How to handle empty resultset

I want to take the second select statement and do something like this but not sure of the syntax and approach in my sp:

If no results returned from select statment Then
@spaceused = 0

-----------------------------------------------------------------
ALTER           PROCEDURE sp_Get_DiskSpaceList_Request
     @ResourceID          int
AS

DECLARE @totalspace int,
      @spaceused  int

SET NOCOUNT ON

-- ### Get Total Hard Disk Space for Current Resource
SELECT  @totalspace = s.DiskSize
FROM    tbl_RServer_PC rp INNER JOIN tbl_DiskSize s ON rp.DiskSizeID = s.DiskSizeID
WHERE   s.DiskSizeID = rp.DiskSizeID AND rp.ResourceID = @ResourceID

-- ### Get DiskSpace used by each user
SELECT  @spaceused = s.DiskSize
FROM    tbl_Resource_Employee he INNER JOIN tbl_DiskSize s ON he.DiskSizeID = s.DiskSizeID
WHERE       he.ResourceID=24
ASKER CERTIFIED SOLUTION
Avatar of adwiseman
adwiseman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lowfatspread
select @totalspace = coalesce(@totalspace,0) - coalesce(@spaceused,0)
 
Avatar of dba123
dba123

ASKER

thanks, didn't think it was so simple as that.
Avatar of dba123

ASKER

shoot, Lowfatspread, didnt' see your post till after...sorry.

can you tell me though what coalesce does?
coalesce(@totalspace,0)

Returns the first non null item in a list,  In this case, your variable is the first item, and the number 0 is the second.

You could replace my line of code

IF @spaceused IS NULL SET @spaceused = 0

With

 SET coalesce(@spaceused, 0)

And you would get the same outcome.
sorry, replace it with
 SET @spaceused = coalesce(@spaceused, 0)
Avatar of dba123

ASKER

thank you