Sql server 2005 SQL Server查询以返回表字段中空内容的百分比

Sql server 2005 SQL Server查询以返回表字段中空内容的百分比,sql-server-2005,stored-procedures,data-mining,data-scrubbing,Sql Server 2005,Stored Procedures,Data Mining,Data Scrubbing,我希望从SQLServer2005上开发的遗留系统中删除迁移项目中的数据,但首要任务是确定哪些列没有真正使用 我的方法背后的一般逻辑是标识大部分为空的列,即表中该列的大多数或所有行都包含空值。这将作为存储过程执行,理想的输出如下: TABLE: contacts (10000 records) --------------------------------- FIELD: id | 0 (0%) Null Records FIELD: username | 0 (0%) Null Record

我希望从SQLServer2005上开发的遗留系统中删除迁移项目中的数据,但首要任务是确定哪些列没有真正使用

我的方法背后的一般逻辑是标识大部分为空的列,即表中该列的大多数或所有行都包含空值。这将作为存储过程执行,理想的输出如下:

TABLE: contacts (10000 records)
---------------------------------
FIELD: id | 0 (0%) Null Records
FIELD: username | 0 (0%) Null Records
FIELD: phonenumber | 8,200 (82%) Null Records
FIELD: email | 300 (3%) Null records
FIELD: icq | 9,900 (99%) Null Records
FIELD: zip | 100 (1%) Null Records
关键是:有些表有100多列,所以真正的关键是让过程循环给定表的列,所以我不必输入一长串列名来运行查询。任何关于如何做到这一点的帮助都会很好


谢谢。

您可以使用列元数据创建如下查询:

select 'select ''' + table_name + ''' table_name, ''' 
+ column_name + ''' column_name, count(*) nullcount '
+ 'from [' + table_name + '] where [' + column_name + '] is null '
from information_schema.columns
select 'companies' table_name, 'id' column_name, count(*) nullcount from [companies] where [id] is null 
union all
select 'companies' table_name, 'code' column_name, count(*) nullcount from [companies] where [code] is null 
union all
...
如果运行上面的查询,您将得到一个select查询列表。将粘贴复制到文本编辑器,并在选择之间插入“全部联合”,如下所示:

select 'select ''' + table_name + ''' table_name, ''' 
+ column_name + ''' column_name, count(*) nullcount '
+ 'from [' + table_name + '] where [' + column_name + '] is null '
from information_schema.columns
select 'companies' table_name, 'id' column_name, count(*) nullcount from [companies] where [id] is null 
union all
select 'companies' table_name, 'code' column_name, count(*) nullcount from [companies] where [code] is null 
union all
...
然后运行这些联合选择

对于表列表,可以对information_schema.tables中的元数据使用相同的技巧


然后使用vlookup在excel中组合这两个列表,或者使用information_schema.tables和information_schema.columns使用子查询构建更复杂的查询。

您可以使用列元数据创建如下查询:

select 'select ''' + table_name + ''' table_name, ''' 
+ column_name + ''' column_name, count(*) nullcount '
+ 'from [' + table_name + '] where [' + column_name + '] is null '
from information_schema.columns
select 'companies' table_name, 'id' column_name, count(*) nullcount from [companies] where [id] is null 
union all
select 'companies' table_name, 'code' column_name, count(*) nullcount from [companies] where [code] is null 
union all
...
如果运行上面的查询,您将得到一个select查询列表。将粘贴复制到文本编辑器,并在选择之间插入“全部联合”,如下所示:

select 'select ''' + table_name + ''' table_name, ''' 
+ column_name + ''' column_name, count(*) nullcount '
+ 'from [' + table_name + '] where [' + column_name + '] is null '
from information_schema.columns
select 'companies' table_name, 'id' column_name, count(*) nullcount from [companies] where [id] is null 
union all
select 'companies' table_name, 'code' column_name, count(*) nullcount from [companies] where [code] is null 
union all
...
然后运行这些联合选择

对于表列表,可以对information_schema.tables中的元数据使用相同的技巧

然后使用vlookup在excel中组合这两个列表,或者使用information\u schema.tables和information\u schema.columns使用子查询构建更复杂的查询。

SELECT*FROM YourDatabase.information\u schema.columns SELECT*FROM YourDatabase.information\u schema.columns