我是SQL和SQL Server 2008 R2的新手。我找到了一种基于游标的方法来查找特定表中包含空值的所有列,但我希望找到一个更简单的基于集合的解决方案。
USE QC_TEST
DECLARE @colName nvarchar(100)
DECLARE @nullCols nvarchar(max)
SELECT @colName = c.name, @nullCols = COALESCE(@nullCols+', ','')+@colName
FROM sys.tables AS t
JOIN sys.columns AS c
ON t.object_id = c.object_id
WHERE t.name = 'myTable'
AND
EXISTS(SELECT * FROM myTable WHERE c.name IS NULL)
SELECT @colName, @nullCols
上面的代码目前返回myTable
中的所有列。如果我将EXISTS
子句更改为NOT EXISTS
,则不返回任何列。结果应该是以逗号分隔的列名字符串,其中至少包含1个空值。
感谢您的帮助
答案 0 :(得分:2)
你可以这样得到一个结果
declare @sql varchar(max), @t varchar(max);
set @t = 'your_table';
select @sql = (select stuff(
(select
'] is null union select ''' + c.name +
''' from ' + @t + ' where [' + c.name
from
(select
c.name
from sys.tables AS t
inner join sys.columns AS c
ON t.object_id = c.object_id
where
t.name = @t
) as c
for xml path('')
), 1, 16, '' ) + '] is null' )
;
exec(@sql);
但我不知道你真的认为那更好。
答案 1 :(得分:0)
您的代码无法正常工作,因为EXISTS子查询只是检查列的名称是否为空,这是不可能的。子查询不会将“WHERE c.name IS NULL”扩展为“WHERE MyColumnName IS NULL”,它只会看到c.name ='MyColumnName',它不为null并且始终返回false。
基于游标的方法可能通过为每列构建单独的字符串查询然后排除字符串来实现。
您可以取消忽略该表并在该结果集中查找空值,即
WITH myTable_unpvt AS (
SELECT ContactID, FieldName, FieldValue
FROM
(SELECT ContactID
, cast(Forename as sql_variant) Forename
, cast(Surname as sql_variant) Surname
, cast(Extn as sql_variant) Extn
, cast(Email as sql_variant) Email
, cast(Age as sql_variant) Age
FROM myTable) p
UNPIVOT
(FieldValue FOR FieldName IN
(Forename, Surname, Extn, Email, Age)
) AS myTable_unpvt
)
SELECT DISTINCT FieldName FROM myTable_unpvt WHERE FieldValue IS NULL
答案 2 :(得分:0)
这有点不同寻常,但试试这个:
--set up test table
--the BEGIN TRY and ALTERs allow you to rerun entire script over and over again
BEGIN TRY
CREATE TABLE YourTable (PK int not null primary key, RowValue1 int null, RowValue2 varchar(5) null, RowValue3 datetime)
END TRY
BEGIN CATCH END CATCH
delete YourTable
ALTER TABLE YourTable ALTER COLUMN RowValue1 int NULL
ALTER TABLE YourTable ALTER COLUMN RowValue2 varchar(5) NULL
ALTER TABLE YourTable ALTER COLUMN RowValue3 datetime NULL
--setup test data
INSERT INTO YourTable VALUES (1,1111,'AAAA',GETDATE())
INSERT INTO YourTable VALUES (2,2222,'BBBB',GETDATE())
INSERT INTO YourTable VALUES (3,3333,'CCCC',GETDATE())
INSERT INTO YourTable VALUES (4,NULL,'DDDD',GETDATE())
INSERT INTO YourTable VALUES (5,5555,'EEEE',NULL)
--determine null columns
DECLARE @BadColumns varchar(50)
BEGIN TRY
ALTER TABLE YourTable ALTER COLUMN RowValue1 int NOT NULL
END TRY
BEGIN CATCH
SET @BadColumns=ISNULL(@BadColumns+', ','')+'RowValue1'
END CATCH
----------------------------------
BEGIN TRY
ALTER TABLE YourTable ALTER COLUMN RowValue2 varchar(5) NOT NULL
END TRY
BEGIN CATCH
SET @BadColumns=ISNULL(@BadColumns+', ','')+'RowValue2'
END CATCH
----------------------------------
BEGIN TRY
ALTER TABLE YourTable ALTER COLUMN RowValue3 datetime NOT NULL
END TRY
BEGIN CATCH
SET @BadColumns=ISNULL(@BadColumns+', ','')+'RowValue3'
END CATCH
--report null columns
SELECT @BadColumns AS [Columns having NULLs]
输出:
Columns having NULLs
--------------------------------------------------
RowValue1, RowValue3
(1 row(s) affected)
如果您因为不知道列名而需要动态,可以在字符串中构建类似的命令,然后执行该命令。