Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how find out unused column in a database (SQL Server 2008 ) .

columns that are left as blank/null/space or zero (given a list of tables).

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

like image 583
user2026061 Avatar asked Jan 31 '13 19:01

user2026061


1 Answers

There is no special procedure inside of SQL Server to make this determination. 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 quirk in the above solution is that any column from a table that is empty will be returned. If you wanted to exclude tables that are empty, you need only adjust 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
                                )';
like image 89
Thomas Avatar answered Sep 28 '22 08:09

Thomas