查找SQL Server中是否有包含空值的列(Find if any columns that contain null in SQL Server)

编程入门 行业动态 更新时间:2024-10-25 03:19:57
查找SQL Server中是否有包含空值的列(Find if any columns that contain null in SQL Server)

我是SQL新手,我在解决这个问题时遇到了一些困难。

我想编写select查询,它返回所有那些有空值的行。

我在桌面上有50多列,并且可以添加一些额外的列,因为这样我就很难写出条件。

我们可以使用AFAIK is null但我不想重复它那么多列。

请帮我解决它。 让我知道是否需要任何其他信息。

I new to SQL, I am having some difficulty solving this problem.

I want to write the select query which returns all those rows which have the null value in it.

I have more than 50 columns on the table and can be added some extra column it and because of that I getting difficult to write where condition.

AFAIK we can use is null but I don't want to repeat it for that much columns.

Please help me solve it. Let me know if any additional information required.

最满意答案

你可以试试这个动态的SQL查询。 如果任何列包含空值,则此查询将返回行。

DECLARE @tb NVARCHAR(255) = N'dbo.[tablename]'; DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM ' + @tb + ' WHERE 1 = 0'; SELECT @sql += N' OR ' + QUOTENAME(name) + ' IS NULL' FROM sys.columns WHERE [object_id] = OBJECT_ID(@tb) AND [is_nullable]=1; EXEC sp_executesql @sql;

You can try this dynamic SQL query. This query will return row if any column contain null value.

DECLARE @tb NVARCHAR(255) = N'dbo.[tablename]'; DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM ' + @tb + ' WHERE 1 = 0'; SELECT @sql += N' OR ' + QUOTENAME(name) + ' IS NULL' FROM sys.columns WHERE [object_id] = OBJECT_ID(@tb) AND [is_nullable]=1; EXEC sp_executesql @sql;

更多推荐

本文发布于:2023-08-07 00:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Server   SQL   Find   null   columns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!