【发布时间】:2023-04-01 13:19:01
【问题描述】:
在 sql 数据库中,我需要一个脚本来选择所有没有空值的行,如下所示:
For all the rows in the database
if row has no null values
select it
列是动态的,我不知道它们的编号或名称
谢谢你
【问题讨论】:
-
您是指特定的表吗?还是整个数据库?你在说什么样的“动态”。包括临时表?
在 sql 数据库中,我需要一个脚本来选择所有没有空值的行,如下所示:
For all the rows in the database
if row has no null values
select it
列是动态的,我不知道它们的编号或名称
谢谢你
【问题讨论】:
这与这个问题Test if any fields are NULL相反。
Martin Smith 修改为查找没有空值的行的答案如下所示。
;with xmlnamespaces('http://www.w3.org/2001/XMLSchema-instance' as ns)
select *
from YourTable as T
where
(
select T.*
for xml path('row'), elements xsinil, type
).exist('//*/@ns:nil') = 0
而Aaron Bertrand修改后提供的答案是……
DECLARE @tb NVARCHAR(255) = N'YourTable';
DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM ' + @tb
+ ' WHERE 1 = 1';
SELECT @sql += N' AND ' + QUOTENAME(name) + ' IS NOT NULL'
FROM sys.columns
WHERE [object_id] = OBJECT_ID(@tb);
EXEC sp_executesql @sql;
【讨论】:
方法:
【讨论】: