SQL Query to check if 100+ (or Dynamic) columns in table is null

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).
This is just a sample table I created to explain this question.
Sample Table with data
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/z17GK.png
In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.
Desired Output
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/prwFo.png
The typical way to do this is to write a query like this.
SELECT * FROM TableName WHERE COALESCE(Col1, Col2, Col3, Col4) is null
If the above table has more 100 columns as I said, writing this query will be hard especially if you have to do it often.
Therefore, I have created a query like this.
DECLARE @query NVARCHAR(MAX);
SELECT @query = ISNULL(@query+', ','') + [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('TableName')
AND [name] != 'PK';
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
EXECUTE(@query)
Does this SQL code follow common best practices? How is the quality of this code? (Ex: Performance)
performance sql sql-server t-sql
add a comment |Â
up vote
2
down vote
favorite
Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).
This is just a sample table I created to explain this question.
Sample Table with data
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/z17GK.png
In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.
Desired Output
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/prwFo.png
The typical way to do this is to write a query like this.
SELECT * FROM TableName WHERE COALESCE(Col1, Col2, Col3, Col4) is null
If the above table has more 100 columns as I said, writing this query will be hard especially if you have to do it often.
Therefore, I have created a query like this.
DECLARE @query NVARCHAR(MAX);
SELECT @query = ISNULL(@query+', ','') + [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('TableName')
AND [name] != 'PK';
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
EXECUTE(@query)
Does this SQL code follow common best practices? How is the quality of this code? (Ex: Performance)
performance sql sql-server t-sql
2
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
1
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
1
Looks good to me
â paparazzo
Apr 19 at 17:50
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).
This is just a sample table I created to explain this question.
Sample Table with data
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/z17GK.png
In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.
Desired Output
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/prwFo.png
The typical way to do this is to write a query like this.
SELECT * FROM TableName WHERE COALESCE(Col1, Col2, Col3, Col4) is null
If the above table has more 100 columns as I said, writing this query will be hard especially if you have to do it often.
Therefore, I have created a query like this.
DECLARE @query NVARCHAR(MAX);
SELECT @query = ISNULL(@query+', ','') + [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('TableName')
AND [name] != 'PK';
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
EXECUTE(@query)
Does this SQL code follow common best practices? How is the quality of this code? (Ex: Performance)
performance sql sql-server t-sql
Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).
This is just a sample table I created to explain this question.
Sample Table with data
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/z17GK.png
In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.
Desired Output
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
Image: https://i.stack.imgur.com/prwFo.png
The typical way to do this is to write a query like this.
SELECT * FROM TableName WHERE COALESCE(Col1, Col2, Col3, Col4) is null
If the above table has more 100 columns as I said, writing this query will be hard especially if you have to do it often.
Therefore, I have created a query like this.
DECLARE @query NVARCHAR(MAX);
SELECT @query = ISNULL(@query+', ','') + [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('TableName')
AND [name] != 'PK';
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
EXECUTE(@query)
Does this SQL code follow common best practices? How is the quality of this code? (Ex: Performance)
performance sql sql-server t-sql
edited Apr 18 at 22:36
choroba
1,13858
1,13858
asked Apr 18 at 22:05
D T
1138
1138
2
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
1
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
1
Looks good to me
â paparazzo
Apr 19 at 17:50
add a comment |Â
2
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
1
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
1
Looks good to me
â paparazzo
Apr 19 at 17:50
2
2
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
1
1
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
1
1
Looks good to me
â paparazzo
Apr 19 at 17:50
Looks good to me
â paparazzo
Apr 19 at 17:50
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Does this SQL code follow common best practices?
No.
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.
2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?
How is the quality of this code?
It looks is fine.
I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Does this SQL code follow common best practices?
No.
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.
2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?
How is the quality of this code?
It looks is fine.
I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
add a comment |Â
up vote
1
down vote
accepted
Does this SQL code follow common best practices?
No.
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.
2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?
How is the quality of this code?
It looks is fine.
I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Does this SQL code follow common best practices?
No.
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.
2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?
How is the quality of this code?
It looks is fine.
I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.
Does this SQL code follow common best practices?
No.
SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';
1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.
2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?
How is the quality of this code?
It looks is fine.
I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.
answered Apr 20 at 13:17
Random_User
1943
1943
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
add a comment |Â
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
1
1
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
I see no problem with select * here. It is built up based on all columns.
â paparazzo
Apr 20 at 16:37
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192409%2fsql-query-to-check-if-100-or-dynamic-columns-in-table-is-null%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference.
â Phrancis
Apr 18 at 23:21
@Phrancis, This is a working code (at least for me). I have written this query to answer a question in StackOverflow. Now I'm having doubt because many experts have recommended the first query even when there are many columns. So, I like to show this query to an expert and get their expertise before I recommend this query to someone else.
â D T
Apr 19 at 6:38
1
@Phrancis We have reviewed TSQL code before.
â paparazzo
Apr 19 at 17:44
1
Looks good to me
â paparazzo
Apr 19 at 17:50