SQL 如果没有返回任何行,请执行此操作 [英] SQL if no rows are returned do this

查看:30
本文介绍了SQL 如果没有返回任何行,请执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 select 语句,我想说如果这个 select 语句不返回任何行,那么在每个单元格中放一个 '' .我该怎么做?

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?

推荐答案

听起来您仍未获得所需的所有行.真的?我认为@Joe Sefanelli 为您的解决方案提供了一个重要的部分,然后提到您需要将 INNER 更改为 LEFT 连接.

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.

这是一个可能的解决方案.将您的 FROM 子句更改为以下内容:

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, ','))
;

通过此更改,您将获得@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.

这篇关于SQL 如果没有返回任何行,请执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆