Following is my table (TestTable) where Column_3 is NULL.
Column_1 Column_2 Column_3
-------- -------- --------
1 2 NULL
1 3 NULL
5 6 NULL
As per functionality, user can select one or more columns.
For example, if user selects Column_3 & Column_2 where Column_3 is NULL. I want to tell user that Column_3 is NULL.
Query : Works for single column
if exists(select * from TestTable where Column_3 is null)
print 'Yes'
else
print 'No'
Result :
Yes
Query : For multiple Columns (not working)
declare @columns nvarchar(max), @tableName nvarchar(max), @query nvarchar(max)
set @columns = 'Column_3, Column_2'
set @tableName = 'TestTable'
set @query = 'select * from (select ' + @columns + ' from ' + @tableName + ') as Result'
print @query
EXEC SP_EXECUTESQL @query
Here
@query = select * from (select Column_3, Column_2 from TestTable) as Result
Above query gives me result for those two columns. I'm not sure how can I check NULL in this query for multiple columns. If I add IS NULL (like I did for single column) after or before last parenthesis it gives me Incorrect syntax near the keyword 'is'. error. How can I achieve my goal in this situation?
if exists(select * from TestTable where Column_3 is null or Column_2 is null)Column_3, Column_2in format.