How do I find columns with just 1 value in each record - for all tables?!?

  • I'm looking at database with 1000 tables and I need to identify the columns where all values are null or all values are the same.

  • The following approach is a simple approach.....although if I had more time I would insert the results into a temp table then select * where the count = 0 (null) or 1(single value).  You may want to do add a table row count and then a count of the values to see if you have a mix of NULL and single value data

    Have fun

    Eric

     

    declare

    @tname sysname

    declare

    @cname sysname

    declare

    @sql varchar(1000)

    declare

    @count int

     

    declare

    TableCursor cursor

    for

    select

    t.name, c.name

    from

    sys.objects t

    inner

    join sys.columns c on c.object_id = t.object_id

    where

    t.type = 'U'

    order

    by t.name, column_id

    open

    TableCursor

    fetch

    TableCursor into @tname, @cname

    while

    @@fetch_status = 0

    begin

    set @sql = 'select ''' + @tname+ ''', ''' + @cname + ''', count(distinct '+ @cname + ' ) from ' + @tname

    execute(@sql)

    fetch TableCursor into @tname, @cname

    end

    close

    TableCursor

    deallocate

    TableCursor

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply