views:

151

answers:

5

I have a select statement and I want to say if this select statement does not return any rows then put a '' in every cell. How do I do this?

+1  A: 

Try this -

IF NOT EXISTS ( SELECT 'x' FROM <TABLE> .... )
BEGIN
    -- Your logic goes here
END
Sachin Shanbhag
The issue is I could have 1 unit that shows data and 1 unit that doesn't show data thus this would only show that one units data but I want both units to show....
@anicolais - For your second unit where you want to show blank data, see answer from tenfour. Would that help?
Sachin Shanbhag
there is the initial query in OP
+4  A: 
select a, b, c from t
if @@rowcount = 0
    select '' as a, '' as b, '' as c

But make sure you understand that '' may have a different datatype than columns a, b, and c.

tenfour
+1  A: 

Put your blank row select at the bottom of a union

select x.JobName , x.Description
from MasterJobList x
where x.IsCycleJob = 1 

union all

select "" , "" 
from MasterJobList x
where not exists
    (
    select 1
    from MasterJobList x
    where x.IsCycleJob = 1 
    )
Rawheiser
+1  A: 

Based on the posted code, I think you're looking to blank out the columns from the UnitType table as that's the only one you're left-joining to. In that case use

ISNULL(ut.[Description], '')  AS UnitType
Joe Stefanelli
This still returns no rows for me if I say u.number = 'e2499' it just gives no rows. I need it to give the unit number but no data.
Then some of your current INNER joins need to be LEFT joins instead, depending on which table(s) you're not finding a matching row in. Then use ISNULL as in my answer to blank those columns as well.
Joe Stefanelli
The table that doesnt have a match row in is IUA so i'm not sure exaclty what I would do here as most have conversion errors when doing ISNULL
ISNULL will return a column of the same datatype as the expression you are testing. Some conversion may be necessary to return '' in those cases.
Joe Stefanelli
+2  A: 

It sounds like you're still not getting all the rows you want. True? I think @Joe Sefanelli provides an important part to your solution, and then mentions that you need to change INNER to LEFT joins.

So, you say you want to display all units in your units list. And, if there's no data for a unit, then display the unit and blanks for the data that doesn't exist.

Here's a possible solution. Change your FROM clause to the following:

FROM  [dbo].[Unit] u 
LEFT OUTER JOIN 
    (
    SELECT *
    FROM [dbo].[IUA] i
    JOIN [dbo].[Reports] r ON r.[Report_ID] = i.[Report_ID]
    JOIN [dbo].[State] s ON i.[St_ID] = s.[St_Id]
    WHERE r.[Account] = [dbo].[fn_Get_PortalUser_AccountNumber](11-11)
        AND r.[Rpt_Period] = '2126'
        AND r.[RptName] = 'tfd'
        AND r.[Type] = 'h'    
    ) ir ON ir.[Unit_ID] = u.[Unit_ID]
LEFT JOIN [dbo].[UnitType] ut ON u.[UnitType] = ut.[UnitType]
WHERE u.[Unit] IN (SELECT [VALUE] 
               FROM dbo.udf_GenerateVarcharTableFromStringList(@Units, ','))
;

With this change you will get a list of units that are in the @Units list. The left outer joins will include data associated with each unit, but will not exclude units if there is no associated data.

bobs
This actually returns all the report periods which would not work becasue I am trying to get only 2126. There is data for the unit in different report periods but not this report period. If I move this into the where clause it still returns no rows again.
Oops. misread your comment about the where clause.
bobs
Yes I believe we are but this returns nothing again....hmmm.
the unit also does not have a unit type...
New version that uses derived table to produce data from [IUA], [Reports] and [STATE]. Then, joins to other tables.
bobs
Msg 206, Level 16, State 2, Line 36Operand type clash: int is incompatible with uniqueidentifierMsg 8156, Level 16, State 1, Line 36The column 'Report_ID' was specified multiple times for 'ir'.
Msg 4104, Level 16, State 1, Line 36The multi-part identifier "s.FuelRate" could not be bound.
For the Msg 206 you will need to change the `SELECT *` to `SELECT` with a list of columns that are referenced elsewhere in the query. This will avoid duplication that is indicated. For Msg 4104, change your SELECT clause to use ir instead of i or s. This will reference the derived table alias.
bobs
in my OP that is how I changed it. It now says Unit_Id is invalid column?
Add `i.[unit_id],` to the derived table `LEFT OUTER JOIN ( SELECT i.[unit_id], i.[Non_Toll],`
bobs
I didnt figure out this way of doing it but you definitely helped me with finding the answer THANKS!!!