如何让所有列(在DB中)只有NULL值。?
如果有2个表格: T1
A B
1 NULL
2 NULL
NULL NULL
和T2
C D
NULL 920
NULL NULL
NULL 2323
我想要返回B和C
答案 0 :(得分:0)
你的意思是B和C吗?
尝试:
Declare @schemaname varchar(255), @tablename varchar(255)
DECLARE table_cur cursor for
SELECT s.name,t.name
FROM sys.tables t Join sys.schemas s on t.schema_id=s.schema_id
OPEN table_cur
FETCH NEXT FROM table_cur into @schemaname, @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
Declare @col varchar(255), @cmd varchar(max)
DECLARE column_cur cursor for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
OPEN column_cur
FETCH NEXT FROM column_cur into @col
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM ' + @schemaname + '.' + @tablename +' WHERE [' + @col + '] IS NOT NULL) BEGIN print '' Table: ' + @tablename + ', Column: ' + @col + ''' end'
EXEC(@cmd)
FETCH NEXT FROM column_cur into @col
END
CLOSE column_cur
DEALLOCATE column_cur
FETCH NEXT FROM table_cur into @schemaname, @tablename
END
CLOSE table_cur
DEALLOCATE table_cur