我尝试检查空值或空列。
SELECT * FROM myTable
WHERE myColumn IS NULL OR myColumn = ' ';
问题是,我想要包含几个表(myTable1,myTable2,myTable3) 在同一个查询中,避免需要指定列名(myColumn) 所以查询是通用的,尽可能在几个表中返回空列或空列名。
提前致谢。
答案 0 :(得分:0)
DECLARE @col VARCHAR(255),
@table VARCHAR(255),
@cmd VARCHAR(max)
DECLARE getinfo CURSOR FOR
SELECT t.name,
c.name
FROM sys.tables t
JOIN sys.columns c
ON t.Object_ID = c.Object_ID
WHERE t.Name IN ( 'table-1', 'table-2' )
--WHERE t.Name IN (provide your TABLE list)
OPEN getinfo
FETCH NEXT FROM getinfo INTO @table, @col
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @cmd = 'IF EXISTS (SELECT top 1 * FROM ' + @table
+ ' WHERE (' + @col + ' IS NULL or ' + @col
+ '='''')) BEGIN print ''tablename=''+'''
+ @table + '''+'' column name=''+''' + @col
+ ''' end'
--PRINT @cmd
EXEC(@cmd)
FETCH NEXT FROM getinfo INTO @table, @col
END
CLOSE getinfo
DEALLOCATE getinfo