如何在 SQL Server 数据库架构中找到所有填充 100% 空值的列?

发布于 2024-09-28 00:38:52 字数 175 浏览 6 评论 0原文

有没有一种 SQL 方法可以找出我的架构中的哪些列完全充满空值?我知道几个表中的某些字段未被应用程序使用,将被删除,但我想看看是否有一种自动化的方法/脚本可以在整个数据库中找到这些字段,以找到代码审查的候选者/可能的删除。

如果有必要的话,在 x86 上运行 SQL Server 2005。

提前致谢!

Is there a SQL way to find out which columns in my schema are completely full of null values? There are some fields in a couple of tables that I know are not used by the application and will be dropped, but I wanted to see if there was an automated way/script to find this out across the whole database to find candidates for code review/possible removal.

Running SQL Server 2005 on x86 if it matters.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中性美 2024-10-05 00:38:52
create table #SuspectColumns (
    TABLE_SCHEMA sysname,
    TABLE_NAME sysname,
    COLUMN_NAME sysname
)

declare csrColumns cursor fast_forward for
    select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
        from INFORMATION_SCHEMA.COLUMNS
        where IS_NULLABLE = 'YES'

declare @TABLE_SCHEMA sysname,
        @TABLE_NAME sysname,
        @COLUMN_NAME sysname,
        @sql nvarchar(max)  

open csrColumns

while (1=1) begin
    fetch next
        from csrColumns
        into @TABLE_SCHEMA, @TABLE_NAME, @COLUMN_NAME

    if @@FETCH_STATUS<>0 break

    set @sql = N'if not exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is not null)
                     insert into #SuspectColumns values (''' + @TABLE_SCHEMA + N''',''' + @TABLE_NAME + N''',''' + @COLUMN_NAME + N''')'

    exec sp_executesql @sql
end /* while */

close csrColumns
deallocate csrColumns

select * from #SuspectColumns

drop table #SuspectColumns
create table #SuspectColumns (
    TABLE_SCHEMA sysname,
    TABLE_NAME sysname,
    COLUMN_NAME sysname
)

declare csrColumns cursor fast_forward for
    select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
        from INFORMATION_SCHEMA.COLUMNS
        where IS_NULLABLE = 'YES'

declare @TABLE_SCHEMA sysname,
        @TABLE_NAME sysname,
        @COLUMN_NAME sysname,
        @sql nvarchar(max)  

open csrColumns

while (1=1) begin
    fetch next
        from csrColumns
        into @TABLE_SCHEMA, @TABLE_NAME, @COLUMN_NAME

    if @@FETCH_STATUS<>0 break

    set @sql = N'if not exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is not null)
                     insert into #SuspectColumns values (''' + @TABLE_SCHEMA + N''',''' + @TABLE_NAME + N''',''' + @COLUMN_NAME + N''')'

    exec sp_executesql @sql
end /* while */

close csrColumns
deallocate csrColumns

select * from #SuspectColumns

drop table #SuspectColumns
百善笑为先 2024-10-05 00:38:52

您可以返回最大值(列)并检查空值

you can return the max(column) and check for nulls

怪我闹别瞎闹 2024-10-05 00:38:52

我突然想到,我相信下面的 SQL 应该可以工作。它将对每个表/列组合执行查询,如果该表/列组合没有行或全部为空行,则查询将返回表名和列名。

DECLARE @table_columns TABLE
(
  table_name nvarchar(128),
  column_name nvarchar(128)
);
DECLARE @table_name nvarchar(128);
DECLARE @column_name nvarchar(128);

INSERT INTO @table_columns(table_name, column_name)
select TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.columns;

while (select count(*) from @table_columns) > 0
begin
    select top 1 @table_name = table_name, @column_name = column_name from @table_columns
    exec('SELECT ''' + @table_name + ''' as table_name, ''' + @column_name + ''' as column_name WHERE NOT EXISTS (SELECT TOP 1 * FROM ' + @table_name + ' WHERE ' + @column_name + ' IS NOT NULL)')
    delete from @table_columns where table_name = @table_name and column_name = @column_name 
end

Off the top of my head, I believe the following SQL should work. It will execute a query for each table/column combination and the query will return the table name and column name if that table/column combination either has no rows or all null rows.

DECLARE @table_columns TABLE
(
  table_name nvarchar(128),
  column_name nvarchar(128)
);
DECLARE @table_name nvarchar(128);
DECLARE @column_name nvarchar(128);

INSERT INTO @table_columns(table_name, column_name)
select TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.columns;

while (select count(*) from @table_columns) > 0
begin
    select top 1 @table_name = table_name, @column_name = column_name from @table_columns
    exec('SELECT ''' + @table_name + ''' as table_name, ''' + @column_name + ''' as column_name WHERE NOT EXISTS (SELECT TOP 1 * FROM ' + @table_name + ' WHERE ' + @column_name + ' IS NOT NULL)')
    delete from @table_columns where table_name = @table_name and column_name = @column_name 
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文