我有以下查询我正在尝试显示消息'此RAO没有子资源',当没有'tblOrganisation.Name'时,请查看下面的查询,它工作正常,但没有显示我的消息没有organisation.name返回
DECLARE @RAOID INT, @ORGID INT
SET @ORGID = 28
SET @RAOID = (SELECT RAOID FROM tblOrganisation WHERE ORGID = @ORGID)
PRINT @RAOID
IF @RAOID IS NOT NULL
BEGIN
SELECT
CASE
WHEN tblOrganisation.ORGID IS NOT NULL THEN tblOrganisation.ORGID
ELSE 'No ORGID'
END AS ORGID,
CASE
WHEN tblOrganisation.Name IS NOT NULL THEN tblOrganisation.Name
ELSE 'No SubSource for this RAO'
END as SUBSOURCENAME
FROM tblOrganisation
LEFT OUTER JOIN tblSubGroup ON tblOrganisation.SubGroupID = tblSubGroup.SubGroupID
WHERE
tblSubGroup.RAOID = @RAOID
END
感谢。
最诚挚的问候, MS
答案 0 :(得分:0)
...
FROM tblOrganisation
LEFT OUTER JOIN
tblSubGroup ON tblOrganisation.SubGroupID = tblSubGroup.SubGroupID
--Need to change WHERE clause
AND
tblSubGroup.RAOID = @RAOID
...
原始的WHERE子句将OUTER JOIN更改为INNER JOIN,因此您需要在那里进行过滤。
另外,其他一些观点:
为什么要加入tblSubGroup?它不用于输出。除非您使用CASE
tblOrganisation.Name是null还是空字符串?
ISNULL(NULLIF(tblOrganisation.Name,''),'此RAO没有子资源')AS ORGID
试试这个,这可能更符合你想要的......
DECLARE @RAOID INT, @ORGID INT
SET @ORGID = 28
SET @RAOID = (SELECT RAOID FROM tblOrganisation WHERE ORGID = @ORGID)
PRINT @RAOID
IF @RAOID IS NOT NULL
BEGIN
SELECT
ISNULL(tblSubGroup.ORGID), 'No ORGID') AS ORGID,
ISNULL(NULLIF(tblSubGroup.Name, ''), 'No SubSource for this RAO') AS SUBSOURCENAME
FROM
tblOrganisation
LEFT OUTER JOIN
tblSubGroup ON tblOrganisation.SubGroupID = tblSubGroup.SubGroupID
AND
tblSubGroup.RAOID = @RAOID
END
然而,我仍然不确定你想要什么...
答案 1 :(得分:0)
DECLARE @RAOID INT, @ORGID INT
SET @ORGID = 28
SET @RAOID = (SELECT RAOID FROM tblOrganisation WHERE ORGID = @ORGID)
PRINT @RAOID
IF @RAOID IS NOT NULL
BEGIN
SELECT
CASE
WHEN tblOrganisation.ORGID IS NOT NULL THEN tblOrganisation.ORGID
ELSE 'No ORGID'
END AS ORGID,
**CASE isnull(tblOrganisation.Name,'')
WHEN '' THEN 'No SubSource for this RAO'
ELSE tblOrganisation.Name**
END as SUBSOURCENAME
FROM tblOrganisation
LEFT OUTER JOIN tblSubGroup ON tblOrganisation.SubGroupID = tblSubGroup.SubGroupID
WHERE
tblSubGroup.RAOID = @RAOID
END
这样做可能对你有用。