tags:

views:

85

answers:

2

I have below query I am trying to show message 'No SubSource for this RAO' when there is no 'tblOrganisation.Name', please have look into below query, it working fine but not showing my message when there no organisation.name returned

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

Thanks.

Best Regards, MS

A: 
...
FROM tblOrganisation 
LEFT OUTER JOIN
tblSubGroup ON tblOrganisation.SubGroupID = tblSubGroup.SubGroupID
--Need to change WHERE clause
    AND
    tblSubGroup.RAOID = @RAOID
...

The original WHERE clause is changing the OUTER JOIN into an INNER JOIN, so you need to filter there.

Also, some other points:

  • Why join on tblSubGroup? It is not used for ouput. Unless you are checking for this with CASE

  • Is tblOrganisation.Name null or empty string?

    ISNULL(NULLIF(tblOrganisation.Name, ''), 'No SubSource for this RAO') AS ORGID

Try this, this may be more what you want...

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

However, I'm still not sure what you want...

gbn
thanks! what should i need to change in where clause? can you please give me the proper code for the same
MKS
its not working for me as without where condition its showing all the oraganisations, as well as I am getting "Unable to convert into char" error on line ISNULL(NULLIF(tblSubGroup.ORGID, ''), 'No ORGID') AS ORGID. could it be possible in my query
MKS
oops. my mistake...
gbn
Dear What was the mistake?
MKS
NULLIF was not needed. I only added that on .Name in case of empty string rather than NULL
gbn
A: 
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

Do like this it may be work for you.

KuldipMCA
You need to learn to highlight your code (SQL or otherwise) and then click on the "code" button (010 1010) on the toolbar to format it nicely!
marc_s
thanks for advice
KuldipMCA