2009-09-02 62 views
0

我有以下查詢我試圖展現消息「不副信號源此RAO」當沒有「tblOrganisation.Name」,請有看到下面的查詢,它做工精細但沒有顯示我的消息時沒有organisation.name返回的Sql ELSE情況下不能正常工作

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

什麼數據庫系統?什麼版本? – 2009-09-02 06:30:56

+0

如果您還有不那麼工作現場不爲空。它回來了什麼? – Donnie 2009-11-29 16:16:51

回答

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 

不要這樣它可能是你的工作。

+1

你需要學習要突出你的代碼(SQL或其他方式),然後單擊工具欄上的「代碼」按鈕(010 1010)很好地格式化! – 2009-09-02 07:28:10

+0

感謝您的建議 – KuldipMCA 2009-09-02 10:58:04

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 

不過,我還是不知道你想要什麼......

+0

謝謝!我應該在where子句中更改哪些內容?你可以請給我正確的代碼相同 – 2009-09-02 06:02:11

+0

對我來說沒有WHERE條件的顯示所有的oraganisations,以及我正在上線ISNULL(NULLIF「無法轉換成字符」錯誤它不工作(tblSubGroup.ORGID ,''),'No ORGID')AS ORGID。有可能在我的查詢 – 2009-09-02 06:14:21

+0

oops。我的錯誤... – gbn 2009-09-02 06:17:00

相關問題