我有一个select语句,如果这个select语句没有返回任何行,则我想在每个单元格中放置''。我该怎么做?
我有一个select语句,如果这个select语句没有返回任何行,则我想在每个单元格中放置''。我该怎么做?
select a, b, c from t
if @@rowcount = 0
select '' as a, '' as b, '' as c
但请确保您理解''
可能与列a
、b
和c
的数据类型不同。
试试这个 -
IF NOT EXISTS ( SELECT 'x' FROM <TABLE> .... )
BEGIN
-- Your logic goes here
END
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
)
听起来你仍然没有得到所有想要的行。是这样吗?我认为@Joe Sefanelli提供了解决方案的重要部分,然后提到你需要将INNER更改为LEFT连接。
所以,你说你想显示单位列表中的所有单位。如果一个单位没有数据,则显示单位和不存在的数据的空白。
以下是可能的解决方案。将你的FROM子句更改为以下内容:
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, ','))
;
SELECT *
更改为在查询中其他地方引用的列的列表。这将避免所指示的重复。对于 Msg 4104,请更改 SELECT 子句以使用 ir 而不是 i 或 s。这将引用派生表别名。 - bobsi.[unit_id],
添加到派生表 LEFT OUTER JOIN ( SELECT i.[unit_id], i.[Non_Toll],
中。 - bobs根据发布的代码,我认为您想要从UnitType表中清除列,因为这是您唯一需要左连接的表。在这种情况下,请使用
ISNULL(ut.[Description], '') AS UnitType
select top 1 isnull(max(col2),' ') as noNullCol from table1 where col1='x'
如果没有行,则 max
函数会返回一个 null
,而 isnull
函数则会返回 ' '
而不是 null
值。
select Company from customer where customer=@Company
union
select '' where not exists (select 1 from customer where customer=@Company)
当没有匹配项时,这将创建一行空白。