TSQL-查找数据库中的所有空列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TSQL-查找数据库中的所有空列相关的知识,希望对你有一定的参考价值。
I found this and simply want to store it. It was done by Valentino Vranken
-- Returns a list of all columns in current database -- where the column's value is null for all records. DECLARE @tempTable TABLE ( TableSchema nvarchar(256), TableName nvarchar(256), ColumnName sysname, NotNullCnt BIGINT ); DECLARE @SQL nvarchar(4000); DECLARE @tableSchema nvarchar(256); DECLARE @tableName nvarchar(256); DECLARE @columnName sysname; DECLARE @cnt BIGINT; DECLARE columnCursor cursor FOR SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE IS_NULLABLE = 'YES'; OPEN columnCursor; fetch NEXT FROM columnCursor INTO @tableSchema, @tableName, @columnName; while @@FETCH_STATUS = 0 BEGIN -- use dynamic sql to get count of records where column is not null SET @SQL = 'select @cnt = COUNT(*) from [' + @tableSchema + '].[' + @tableName + '] where [' + @columnName + '] is not null'; -- print @sql; --uncomment for debugging EXEC sp_executesql @SQL, N'@cnt bigint output', @cnt = @cnt output; INSERT INTO @tempTable SELECT @tableSchema, @tableName, @columnName, @cnt; fetch NEXT FROM columnCursor INTO @tableSchema, @tableName, @columnName; END close columnCursor; deallocate columnCursor; SELECT * FROM @tempTable WHERE NotNullCnt = 0;
以上是关于TSQL-查找数据库中的所有空列的主要内容,如果未能解决你的问题,请参考以下文章
如何将默认值 0 赋予 Handsontable 中的所有空单元格?