Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a NULL instead of No Value from a query

I had a query like:

SELECT ISNULL(S.Name+'.'+T.Name,'Table Not Found')
FROM DataProfile.Tables T
INNER JOIN DataProfile.Schemas S ON T.schemaId=S.Id
WHERE S.Name+'.'+T.Name=@TableName

Then I tried

IIF(LEN(S.Name+'.'+T.Name)>0,S.Name+'.'+T.Name,NULL)

But when it doesn't find the named table returns not output, Value or Null value or anything I can work on. This is going to be used as a crosscheck.
Does anybody have any idea?

like image 538
El.Hum Avatar asked Jan 04 '23 03:01

El.Hum


2 Answers

Thanks for those who payed attention to what I exactly asked and for their responses. Here the way I tried:

DECLARE @Check NVARCHAR(MAX) = 'TABLE DOES NOT FOUND'

SELECT @Check= S.Name + '.' + T.Name
FROM DataProfile.Tables T
INNER JOIN DataProfile.Schemas S ON T.schemaId=S.Id
WHERE S.Name+'.'+T.Name=@TableName

SELECT @CHECK

And That Worked for me

like image 117
El.Hum Avatar answered Jan 05 '23 18:01

El.Hum


this will always return a row:

select v.TableName, ISNULL(found.result, 'not found') result
from (values(@TableName))v(TableName)
    outer apply (
        select CAST('found' as nvarchar(11)) result
        from DataProfile.Tables T
            join  DataProfile.Schemas S ON T.schemaId=S.Id
        where S.Name+'.'+T.Name=v.TableName
    )found
like image 37
avb Avatar answered Jan 05 '23 16:01

avb