How to find out an unused column in a database (SQL Server 2008).

which remain empty / zeros / spaces or zeros (given the list of tables).

Assuming I can derive table and column definitions from central system tables, how should I check the above condition?

+5
source share
1 answer

There is no special procedure in SQL Server to complete this definition. You need to query each table. Here's a brute force solution:

If object_id('tempdb..#Results') is not null
    Drop Table #Results;
GO
Create Table #Results
    (
    TableSchema sysname not null
    , TableName sysname not null
    , ColumnName sysname not null
    );
GO

Declare @TableSchema sysname;
Declare @TableName sysname;
Declare @ColumnName sysname;
Declare @DataType sysname;
Declare @Columns Cursor;
Declare @BaseSql nvarchar(max);
Declare @Sql nvarchar(max);
Declare @AdditionalFilter nvarchar(max);

Set @BaseSql = 'Insert #Results( TableSchema, TableName, ColumnName )
                Select ''TABLE_SCHEMA'', ''TABLE_NAME'', ''COLUMN_NAME''
                From ( Select 1 As V ) As Z
                Where Not Exists    (
                                    Select 1
                                    From [TABLE_SCHEMA].[TABLE_NAME]
                                    Where [COLUMN_NAME] Is Not Null
                                        ADDITIONAL_FILTER
                                    )';

Set @Columns = Cursor Fast_Forward For
    Select C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE
    From INFORMATION_SCHEMA.COLUMNS As C
        Left Join INFORMATION_SCHEMA.VIEWS As V
            On V.TABLE_SCHEMA = C.TABLE_SCHEMA
                And V.TABLE_NAME = C.TABLE_NAME
    Where V.TABLE_NAME Is Null;

Open @Columns;
Fetch Next From @Columns Into @TableSchema, @TableName, @ColumnName, @DataType;

While @@Fetch_Status = 0
Begin
    If @DataType In('int','smallint','tinyint','bigint','numeric','bit','decimal','money','smallmoney','float','real')
        Set @AdditionalFilter = 'And [COLUMN_NAME] <> 0';
    Else If @DataType In('char','nchar','varchar','nvarchar','text','ntext')
        Set @AdditionalFilter = 'And Len([COLUMN_NAME]) > 0';
    Else
        Set @AdditionalFilter = '';

    Set @Sql = Replace(@BaseSql, 'ADDITIONAL_FILTER', @AdditionalFilter);
    Set @Sql = Replace(@Sql, 'TABLE_SCHEMA', @TableSchema);
    Set @Sql = Replace(@Sql, 'TABLE_NAME', @TableName);
    Set @Sql = Replace(@Sql, 'COLUMN_NAME', @ColumnName);

    --Print @Sql
    Exec(@Sql)
    Fetch Next From @Columns Into @TableSchema, @TableName, @ColumnName, @DataType;
End

Close @Columns;
Deallocate @Columns;

Select *
From #Results

One mistake in the solution above is that any column from an empty table will be returned. If you want to exclude empty tables, you only need to configure the query to the following:

Set @BaseSql = 'Insert #Results( TableSchema, TableName, ColumnName )
                Select ''TABLE_SCHEMA'', ''TABLE_NAME'', ''COLUMN_NAME''
                From ( Select 1 As V ) As Z
                Where Exists    (
                                Select 1
                                From [TABLE_SCHEMA].[TABLE_NAME]
                                )
                And Not Exists  (
                                Select 1
                                From [TABLE_SCHEMA].[TABLE_NAME]
                                Where [COLUMN_NAME] Is Not Null
                                    ADDITIONAL_FILTER
                                )';
+9
source

All Articles